重载视图模型构造和HttpPost在ASP.NET MVC [英] Overloaded ViewModel Constructor and HttpPost in ASP.NET MVC

查看:88
本文介绍了重载视图模型构造和HttpPost在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的StackOverflow发现我有什么最近的问题是,
<一href=\"http://stackoverflow.com/questions/9413313/posting-data-when-my-view-model-has-a-constructor-does-not-work\">Posting当我的视图模型有一个构造数据不起作用

The closest question I found in StackOverflow to what I have is Posting data when my view model has a constructor does not work

型号

    public class Customer
    {
        public int Id { get; set; }                
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

视图模型

    public class CustomerViewModel
    {
        public Customer Customer { get; set; }

        public CustomerViewModel(Customer customer)
        {
            Customer = customer;        
        }
    }

控制器code

    public ActionResult CreateCustomer()
    {
        Customer c = new Customer();
        CustomerViewModel cvm = new CustomerViewModel(c);
        return View(cvm);
    }

    [HttpPost]
    public ActionResult CreateCustomer(CustomerViewModel customer)
    {
       // do something here
    }

查看code

    @model Blah.Models.CustomerViewModel

    @{
        ViewBag.Title = "CreateCustomer";
     }

    <h2>CreateCustomer</h2>

    @using (Html.BeginForm()) 
    { 
        <div class="editor-label">
          @Html.LabelFor(model => model.Customer.FirstName)
        </div>
        <div class="editor-field">
          @Html.EditorFor(model => model.Customer.FirstName)
          @Html.ValidationMessageFor(model => model.Customer.FirstName)
        </div>

        <div class="editor-label">
          @Html.LabelFor(model => model.Customer.LastName)
        </div>
        <div class="editor-field">
          @Html.EditorFor(model => model.Customer.LastName)
          @Html.ValidationMessageFor(model => model.Customer.LastName)
        </div>

        <p>
          <input type="submit" value="Create" />
        </p>
    }

错误

解决方案,刚刚摆脱错误的,但没有帮助


  • 添加默认的构造函数(参数为空 - 不符合我的目的)

  • 不要有重载的构造函数(我的型号是空的话)

我想我需要一个自定义的模型绑定在这里。不知道如何创建一个: - (

I guess I need a custom model binder here. Don't know how to create one :-(

(或)

我想知道我有什么其他选择此处

I would like to know what other options I have here

推荐答案

您需要添加一个参数的构造函数。

You need to add a parameterless constructor.

public class CustomerViewModel
{
    public Customer Customer { get; set; }

    public CustomerViewModel()
    {
    }
    public CustomerViewModel(Customer customer)
    {
        Customer = customer;        
    }
}

您认为这是不工作的原因是另一个问题。您的模型有一个命名属性客户这是一个复杂类型和POST方法的参数也被命名为客户(在 DefaultModelBinder 不区分大小写)。其结果是,绑定失败。您需要将参数名称更改为比你的属性之一的名称,例如:

The reason you think this is 'not working' is another issue. Your model has a property named Customer which is a complex type and the parameter of your POST method is also named customer (the DefaultModelBinder is not case sensitive). As a result, binding fails. You need to change the parameter name to anything other than the name of one of your properties, for example

[HttpPost]
public ActionResult CreateCustomer(CustomerViewModel model)

这篇关于重载视图模型构造和HttpPost在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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