在 MVC PartialView 中获取发布的值

发布于 2024-08-10 21:01:30 字数 646 浏览 3 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法少女 2024-08-17 21:01:30

您需要发布实际的表单元素,以便任何人都知道出了什么问题。

html 表单用于设置与 SearchDomain 的绑定。您希望表单元素的命名如下:

<input name="searchData.SomeProperty">

让它们绑定到您的操作参数。

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:

<input name="searchData.SomeProperty">

For them to bind to your action parameter.

深海蓝天 2024-08-17 21:01:30

为了通过控制器方法将 SearchDomain 对象从视图中拉出,您的视图必须继承自 System.Web.Mvc.ViewPage,或包含 SearchDomain 对象的 自定义 ViewModel 类

另一种方法是让您的视图继承 System.Web.Mvc.ViewPage,并使用 UpdateModel 将视图数据转换为 SearchDomain 对象。像这样的东西:

public ActionResult Save()
{
        SearchDomain domain = new SearchDomain ();

        UpdateModel(domain , new[] { "Name", "Email", "Phone", ... });
        return View(domain); 
}

In order to pull a SearchDomain object out of your view from a controller method, your view has to either inherit from System.Web.Mvc.ViewPage<Models.SearchDomain>, or a custom ViewModel class that contains a SearchDomain 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 a SearchDomain object. Something like this:

public ActionResult Save()
{
        SearchDomain domain = new SearchDomain ();

        UpdateModel(domain , new[] { "Name", "Email", "Phone", ... });
        return View(domain); 
}
谁人与我共长歌 2024-08-17 21:01:30

老实说,我认为 RenderAction 使用起来更加方便。

To be honest, I think RenderAction is much easier to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文