Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 单击按钮更新和更新ASP.NET MVC模型_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 单击按钮更新和更新ASP.NET MVC模型

Asp.net mvc 单击按钮更新和更新ASP.NET MVC模型,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我是ASP.NETMVC新手。我试图在单击按钮时更新模型,但没有成功:每次我按下按钮时,都会调用一个HttpGet控制器方法 这是我的标记 @model DataInterface.Model.Entry <button onclick="location.href='@Url.Action("Survey")'">Finish survey</button> 当我单击按钮时,会调用HttpGet方法。为什么? (作为一个新手是不好的) 谢谢大家 @使用(Html.

我是ASP.NETMVC新手。我试图在单击按钮时更新模型,但没有成功:每次我按下按钮时,都会调用一个HttpGet控制器方法

这是我的标记

@model DataInterface.Model.Entry

<button onclick="location.href='@Url.Action("Survey")'">Finish survey</button>
当我单击按钮时,会调用HttpGet方法。为什么?


(作为一个新手是不好的) 谢谢大家

@使用(Html.BeginForm(“Survey”,“”,FormMethod.Post))
   @using (Html.BeginForm("Survey", "<ControllerName>", FormMethod.Post))
   {
       <input  type="submit" value="Finish survey" />    
   }
{ }
您必须声明您的表单

@model DataInterface.Model.Entry
@using (Html.BeginForm("action", "Controlleur", FormMethod.Post, new {@class = "form", id = "RequestForm" }))
{
<input  type="submit" value="Finish survey" />
}
@model DataInterface.model.Entry
@使用(Html.BeginForm(“action”,“controller”,FormMethod.Post,new{@class=“form”,id=“RequestForm”}))
{
}

如果访问
URL
时未明确指定
HTTP方法
ASP.NET MVC
将假定一个
GET
请求。要更改此设置,您可以添加表单并发送:

@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    <input type="submit" value="Finish survey" />    
}
如果您将对象存储在服务器上的某个位置,并且只想通过将其写入数据库或更改其状态来完成它,则可以沿着post请求传递对象的
Id
,并使控制器方法仅使用Id:

@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    @Html.HiddenFor(m => m.Id)
    <input type="submit" value="Finish survey" />    
}

[HttpPost]
public ActionResult Survey(Entry newEntry)
{
    // newEntry.Id will be set here
}
@使用(Html.BeginForm(“调查”、“控制者”、FormMethod.Post))
{
@Html.HiddenFor(m=>m.Id)
}
[HttpPost]
公共行动结果调查(新条目)
{
//newEntry.Id将在此处设置
}

嗯,这正是您告诉浏览器要做的事情。单击按钮时,浏览器的URL将更改为指向控制器中的
测量
方法。浏览器将向其发送get请求并处理响应。发送
POST
请求的最简单方法是提交表单。旁注:由于视图是由
Survey()
方法生成的,因此只需使用(Html.BeginForm()){…。-如果要覆盖默认值,只需包含参数
@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.Title)
    <input type="submit" value="Finish survey" />    
}
@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
    @Html.HiddenFor(m => m.Id)
    <input type="submit" value="Finish survey" />    
}

[HttpPost]
public ActionResult Survey(Entry newEntry)
{
    // newEntry.Id will be set here
}