如何从 Ajax.BeginForm 发布完整的 DropDownList 而不仅仅是 selectedId
//Model
public class SelectModel
{
public string CategoryId { get; set; }
public List<SelectListItem> List { get; set; }
}
//ViewModel
public class ViewModel
{
public SelectModel SelectMod { get; set; }
}
//OnIndex call
@Html.Partial("ViewUserControl1", Model.SelectMod)
//ViewUserControl1.cshtml
@model MvcApplication4.Models.SelectModel
<div id="formid">
@using (Ajax.BeginForm("Index1", "Home", new AjaxOptions { UpdateTargetId = "formid" }, new { id = "TheForm" }))
{
@Html.DropDownListFor(x => x.CategoryId, Model.List, "--Select One--")
<input type="submit" name="name" value="Submit" />
}
</div>
//Controller
public ActionResult Index1(SelectModel sm)
{
return PartialView("ViewUserControl1", sm);
}
因此,现在当您发布 Ajax.BeginForm 时,我们将获得表单的 selectedId,而不是完整的下拉列表。 如果我有很多下拉列表,我是否需要使用完整的下拉列表再次创建 sm。 有没有一种方法可以将整个下拉列表发送到控制器并返回相同的内容。
更新: AjaxOptions 中不应该有一个参数,它允许我们发布页面的完整列表(如果需要)以及 selectedIds。
//Model
public class SelectModel
{
public string CategoryId { get; set; }
public List<SelectListItem> List { get; set; }
}
//ViewModel
public class ViewModel
{
public SelectModel SelectMod { get; set; }
}
//OnIndex call
@Html.Partial("ViewUserControl1", Model.SelectMod)
//ViewUserControl1.cshtml
@model MvcApplication4.Models.SelectModel
<div id="formid">
@using (Ajax.BeginForm("Index1", "Home", new AjaxOptions { UpdateTargetId = "formid" }, new { id = "TheForm" }))
{
@Html.DropDownListFor(x => x.CategoryId, Model.List, "--Select One--")
<input type="submit" name="name" value="Submit" />
}
</div>
//Controller
public ActionResult Index1(SelectModel sm)
{
return PartialView("ViewUserControl1", sm);
}
So now when you post the Ajax.BeginForm we get the selectedId of the form and not the FULL DROPDOWN LIST.
If i have many dropdown list do i need to create the sm again with full dropdown list.
Is there a way where i can send the whole dropdown list to the controller and return back the same.
Update :
Should't there be a param in AjaxOptions which allows us to post the full list(if we need) of the page along with the selectedIds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是否定的!由于 HTML 中的下拉列表不会返回所有成员,仅返回选定的成员。
更长的答案是,ASP.NET WebForms 允许您执行您所要求的操作,并且您可以通过创建与 ASP.NET WebForms 中的 ViewState 类似的机制来重新创建其中的一些内容。
ASP.NET WebForms 的作用是将列表中的所有值存储在页面中两次。两者都在列表中用于显示,并且还有一个名为 ViewState 的隐藏字段中的副本。然后,隐藏字段在提交时发送回服务器并用于重新填充下拉列表。
您可以通过以某种方式序列化集合并将其放入隐藏字段来模仿 ASP.NET WebForms 在 MVC 中使用的相同行为。然后在提交时,您可以将隐藏字段中的数据反序列化回集合中,并使用它来填充下拉列表。
The short answer is no! Since dropdownlists in HTML don't return all their members, just the selected one(s).
The longer answer is that ASP.NET WebForms allows you to do what you ask for, and you could recreate some of that by creating a similar mechanism as the ViewState is in ASP.NET WebForms.
What ASP.NET WebForms does is that it stores all the values from the list in the page twice. Both in the list for display, and also a copy in a hidden field called the ViewState. The hidden field is then sent back to the server on submit and used to repopulate the dropdownlist.
You could mimick the same behaviour ASP.NET WebForms uses in MVC by serializing your collection in some way and putting it in a hidden field. Then on submit, you can deserialize the data from the hidden field back into the collection and use it to populate the dropdown.