单击按钮时更新和 ASP.NET MVC 模型
Update and ASP.NET MVC model on button click
我是 ASP.NET MVC 的新手。我尝试在单击按钮时更新模型但没有成功:每次我按下按钮时都会调用 HttpGet 控制器方法。
这是我的标记
@model DataInterface.Model.Entry
<button onclick="location.href='@Url.Action("Survey")'">Finish survey</button>
这是控制器代码
[HttpGet]
public ActionResult Survey()
{
var entry = new Entry();
return View(entry);
}
[HttpPost]
public ActionResult Survey(Entry newEntry)
{
// save newEntry to database
}
当我单击按钮时,将调用 HttpGet 方法。为什么?
当菜鸟不好)
感谢大家!
@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" />
}
如果您在未明确指定 HTTP method
的情况下访问 URL
,ASP.NET MVC
将假定为 GET
请求。要更改此设置,您可以添加表单并发送:
@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
<input type="submit" value="Finish survey" />
}
如果您这样做,您的 POST
方法将被调用。但是,Entry
参数将为空,因为您没有指定要随请求一起发送的任何值。最简单的方法是指定输入字段,例如文本输入、下拉菜单、复选框等
@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
@Html.TextBoxFor(m => m.Title)
<input type="submit" value="Finish survey" />
}
如果您将对象存储在服务器的某处并且只想通过将其写入数据库或更改其状态来完成它,您可以传递对象的 Id
(或一些临时 ID ) 沿着 post 请求并使控制器方法仅与 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
}
我是 ASP.NET MVC 的新手。我尝试在单击按钮时更新模型但没有成功:每次我按下按钮时都会调用 HttpGet 控制器方法。
这是我的标记
@model DataInterface.Model.Entry
<button onclick="location.href='@Url.Action("Survey")'">Finish survey</button>
这是控制器代码
[HttpGet]
public ActionResult Survey()
{
var entry = new Entry();
return View(entry);
}
[HttpPost]
public ActionResult Survey(Entry newEntry)
{
// save newEntry to database
}
当我单击按钮时,将调用 HttpGet 方法。为什么?
当菜鸟不好) 感谢大家!
@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" />
}
如果您在未明确指定 HTTP method
的情况下访问 URL
,ASP.NET MVC
将假定为 GET
请求。要更改此设置,您可以添加表单并发送:
@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
<input type="submit" value="Finish survey" />
}
如果您这样做,您的 POST
方法将被调用。但是,Entry
参数将为空,因为您没有指定要随请求一起发送的任何值。最简单的方法是指定输入字段,例如文本输入、下拉菜单、复选框等
@using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
@Html.TextBoxFor(m => m.Title)
<input type="submit" value="Finish survey" />
}
如果您将对象存储在服务器的某处并且只想通过将其写入数据库或更改其状态来完成它,您可以传递对象的 Id
(或一些临时 ID ) 沿着 post 请求并使控制器方法仅与 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
}