在 MVC PartialView 中获取发布的值
我创建了一个 PartialView,用 Html.RenderPartial 渲染,传递视图的名称和要绑定到的强类型数据项(如下):
<% Html.RenderPartial("SearchViewUserControl", ViewData["SearchData"]); %>
部分视图有一个包含提交按钮的表单:
<% using (Html.BeginForm("Search", "Home"))
{ %>
...
<div>
<input type="submit" value="Search" />
</div>
<% } %>
我设置了一个我的控制器的操作方法中设置了断点(如下),但 searchData 中未设置任何内容。我做错了什么?
public ActionResult Search(SearchDomain searchData)
{
if (ModelState.IsValid)
{
}
return View();
}
I've created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below):
<% Html.RenderPartial("SearchViewUserControl", ViewData["SearchData"]); %>
The partial view has a form containing a submit button:
<% using (Html.BeginForm("Search", "Home"))
{ %>
...
<div>
<input type="submit" value="Search" />
</div>
<% } %>
I've set a breakpoint in my controller's action method (below) but nothing is set in searchData. What am I doing wrong?
public ActionResult Search(SearchDomain searchData)
{
if (ModelState.IsValid)
{
}
return View();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要发布实际的表单元素,以便任何人都知道出了什么问题。
html 表单用于设置与 SearchDomain 的绑定。您希望表单元素的命名如下:
让它们绑定到您的操作参数。
You need to post the actual form elements for anybody to know whats wrong.
The form html is what sets the binding to SearchDomain. You want to have your form elements named like this:
For them to bind to your action parameter.
为了通过控制器方法将
SearchDomain
对象从视图中拉出,您的视图必须继承自System.Web.Mvc.ViewPage,
或包含SearchDomain
对象的 自定义 ViewModel 类。另一种方法是让您的视图继承
System.Web.Mvc.ViewPage
,并使用 UpdateModel 将视图数据转换为SearchDomain
对象。像这样的东西:In order to pull a
SearchDomain
object out of your view from a controller method, your view has to either inherit fromSystem.Web.Mvc.ViewPage<Models.SearchDomain>,
or a custom ViewModel class that contains aSearchDomain
object.The other way to do it is to have your view inherit from
System.Web.Mvc.ViewPage
, and use UpdateModel to cast the view data to aSearchDomain
object. Something like this:老实说,我认为 RenderAction 使用起来更加方便。
To be honest, I think RenderAction is much easier to use.