强类型视图模型在POST操作方法,只包含空值 [英] Strongly typed ViewModel in POST action method contains only null values

查看:171
本文介绍了强类型视图模型在POST操作方法,只包含空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现与接收自定义形状的ViewModel类的强类型视图我编辑的操作方法。换句话说,我希望有一个强类型的视图模型,其中包含应编辑的LINQ的实体加上应在视图中显示一些其他对象。

调用GET编辑操作方法时,我能看到的看法,但强类型的POST操作方法接收与空参数只是一个ViewModel类,我无法弄清楚如何获取POST参数。

视图模型如下所示:

  //我的自定义形状的视图模型
公共类CustomersFormViewModel
{
    公众的SelectList AccountTypesDropDownBox;
    公众的SelectList CountriesDropDownBox;
    公众客户客户;
}

操作方法如下所示:

  //
// GET:/ CustomersController /编辑公众的ActionResult编辑(INT ID)
{
   VAR模型=新CustomersFormViewModel
                    {
                        客户= repository.Load(ID.Value)
                        CountriesDropDownBox =的getCountries(),
                        AccountTypesDropDownBox = GetAccountTypes()
                    };
    返回查看(模型);
}//
// POST:/ CustomersController /编辑的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult编辑(CustomersFormViewModel模型)
{
    //下一行抛出!
    Debug.Assert的(Model.Customer!= NULL);
    返回查看(模型);
}

这是我编辑观点:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/ CustAdminMaster.master
    Inherits=\"System.Web.Mvc.ViewPage<Zeiterfassung.Controllers.CustomersController+CustomersFormViewModel>\" %GT;&LT; ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器&GT;
    NewEdit
&LT; / ASP:内容&GT;
&LT; ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器&GT;
    &LT; H2&GT;
        NewEdit&LT; / H&GT;
    &LT;%= Html.ValidationSummary(编辑不成功,请更正错误,然后再试一次。)%&GT;
    &LT;使用%(Html.BeginForm())
       {%GT;
    &LT;&字段集GT;
        &LT;传奇&GT;&领域LT; /传说&GT;
        &所述p为H.;
            &LT;标签=名字&GT;姓:&LT; /标签&gt;
            &LT;%= Html.TextBox(名字,Model.Customer.FirstName)%GT;
            &所述;%= Html.ValidationMessage(姓,*)%&GT;
        &所述; / P&GT;
        &所述p为H.;
            &LT;输入类型=提交值=保存/&GT;
        &所述; / P&GT;
    &LT; /字段集&GT;
    &LT;%}%GT;
    &LT; D​​IV&GT;
        &LT;%= Html.ActionLink(返回目录,索引)%GT;
    &LT; / DIV&GT;
&LT; / ASP:内容&GT;

我也尝试了POST操作方法与参数formValues​​,但该视图模型仍然没有包含发布参数:

 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult编辑(INT ID,的FormCollection formValues​​)
{
      CustomersFormViewModel模式=新CustomersFormViewModel();
      的UpdateModel(模型);
      //下一行STILL抛出
      Debug.Assert的(model.Customer!= NULL);
      返回视图(NewEdit模型);
}

到目前为止,我发现的唯一办法就是写我自己的code,它抓住从的FormCollection张贴的参数,并相应地更新我的自定义视图模型。但这种做法似乎有点原始。难道真的没有更好的办法来做到这一点?

编辑:我只是试图在视图不同的语法tvanfosson建议,但问题仍然不变:

 &LT;标签=Customer.FirstName&GT;姓:&LT; /标签&gt;
    &所述;%= Html.TextBox(Customer.FirstName)%&GT;
    &所述;%= Html.ValidationMessage(Customer.FirstName,*)%&GT;


解决方案

您需要命名的字段按照模型中的prefixes。此外,您将需要修改使用属性而不是字段中输入您的视图模型。默认的模型绑定唯一绑定时看模型上的公共属性。

 &LT;标签=Customer.FirstName&GT;姓:&LT; /标签&gt;
    &所述;%= Html.TextBox(Customer.FirstName)%&GT;
    &所述;%= Html.ValidationMessage(Customer.FirstName,*)%&GT;

这样,它的模型绑定知道如何用适当的组件的形式参数和模型的关联的属性联系起来。

I am trying to implement my Edit action methods with strongly typed view which receives a custom-shaped ViewModel class. In other words, I want a strongly typed ViewModel which contains the Linq entity that should be edited plus a few other objects that should be displayed in the View.

I can see the view when calling the GET Edit action method, but the strongly typed POST action method receives only a ViewModel class with null parameters and I can't figure out how to retrieve the POST parameters.

The View Model looks as follows:

//my custom-shaped ViewModel
public class CustomersFormViewModel
{
    public SelectList AccountTypesDropDownBox;
    public SelectList CountriesDropDownBox;
    public Customer Customer;
}

The action method looks as follows:

//
// GET: /CustomersController/Edit   

public ActionResult Edit(int ID)
{
   var model = new CustomersFormViewModel
                    {
                        Customer = repository.Load(ID.Value),
                        CountriesDropDownBox = GetCountries(),
                        AccountTypesDropDownBox = GetAccountTypes()
                    };
    return View(model);
}

//
// POST: /CustomersController/Edit  

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomersFormViewModel model)
{
    //THE NEXT LINE THROWS!!!
    Debug.Assert(Model.Customer!=null);
    return View(model);
}

And this is my Edit view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/CustAdminMaster.master"
    Inherits="System.Web.Mvc.ViewPage<Zeiterfassung.Controllers.CustomersController+CustomersFormViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    NewEdit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        NewEdit</h2>
    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
    <% using (Html.BeginForm())
       {%>
    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="FirstName">FirstName:</label>
            <%= Html.TextBox("FirstName",Model.Customer.FirstName) %>
            <%= Html.ValidationMessage("FirstName", "*") %>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
    <% } %>
    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>
</asp:Content>

I've also tried a POST action method with formValues parameters, but the the viewmodel still did not contain the posted parameters:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int ID, FormCollection formValues)
{
      CustomersFormViewModel model = new CustomersFormViewModel();            
      UpdateModel(model);
      //THE NEXT LINE STILL THROWS
      Debug.Assert(model.Customer!=null);
      return View("NewEdit",model);
}

The only way I have found so far is to write my own code that grab the posted parameters from the FormCollection and updates my custom ViewModel accordingly. But this approach seems a bit primitive. Is there really no better way to do this?

EDIT: I've just tried a different syntax in the view as tvanfosson suggested, but the problem remains unchanged:

<label for="Customer.FirstName">FirstName:</label>
    <%= Html.TextBox("Customer.FirstName") %>
    <%= Html.ValidationMessage("Customer.FirstName", "*") %>

解决方案

You need to name your fields in accordance with the prefixes in the model. In addition, you will need to modify your view model to use properties instead of fields. The default model binder only looks at the public properties on the model when binding.

    <label for="Customer.FirstName">FirstName:</label>
    <%= Html.TextBox("Customer.FirstName") %>
    <%= Html.ValidationMessage("Customer.FirstName", "*") %>

That way it the model binder knows how to associate the form parameter with the appropriate component and associated property of your model.

这篇关于强类型视图模型在POST操作方法,只包含空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆