我有一个很长的表单,已使用部分 View 分成单独的页面。当用户单击转到下一个局部 View 表单时,我想保存输入到一个局部 View 表单中的数据。然后,当他们返回到上一个 View 时,输入的数据就会在那里。
所以我需要的是将数据发布到 Controller 。 虽然我使用的部分 View 是:
@Ajax.ActionLink("Job Information", "JobInformation", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "jobForm"})
当他们单击操作链接时,会将他们带到下一页(部分 View )
那么当他们点击操作链接时我如何提交表单?
最佳答案
how can i submit the form when they click the action link?
i need is to post the data to a controller.
不使用 AJAX ActionLink,而是使用 AJAX BeginForm,如下所示。假设您有一个如下所示的模型来填充下拉列表。
public class DDLModel
{
public List<SelectListItem> Items { get; set; }
public string SelectedValue { get; set; }
}
然后你就有 Controller 来显示这个模型 -
@model MVC.Controllers.DDLModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
@using (Ajax.BeginForm("Submit", "Ajax", new AjaxOptions { UpdateTargetId = "SellerWebSettings" }, new { id = "form1" }))
{
@Html.DropDownListFor(m => m.SelectedValue, Model.Items, "DDL")
<input type="submit" value="Click" />
}
<div id="SellerWebSettings">
</div>
在上面的表单中,您有一个 AJAX BeginForm() ,它会将 AJAX 全部发送到另一个 Controller 操作,该操作将返回 PartialView。模型也会被转发到局部 View ,如下所示 -
public ActionResult Submit(DDLModel model)
{
return PartialView("MyPartial",model);
}
部分 View 如下 -
@model MVC.Controllers.DDLModel
<div>
@DateTime.Now - @Model.SelectedValue
</div>
因此,当我们选择下拉列表项并单击按钮时,我们会得到以下输出 -
关于asp.net-mvc - 如何在asp.net mvc页面之间保存表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21916898/