Asp.net mvc 单击按钮更新和更新ASP.NET MVC模型
我是ASP.NETMVC新手。我试图在单击按钮时更新模型,但没有成功:每次我按下按钮时,都会调用一个HttpGet控制器方法 这是我的标记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.
@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
}