asp.Net MVC视图模型是空的后 [英] asp.Net MVC view model is empty on post

查看:197
本文介绍了asp.Net MVC视图模型是空的后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我传递到视图创建一个复杂的视图模型。当我在网页上输入数据,并张贴模式是空的。无论是在子对象和测试字段的字段是空的。为什么呢?

 公共类ContactIncident
{
    [键]
    公众诠释标识{搞定;组; }    [数据类型(DataType.MultilineText)
    公共字符串描述{搞定;组; }    [显示(NAME =事故日期)]
    [数据类型(DataType.Date)
    公众的DateTime? IncidentDateTime {搞定;组; }    [显示(名称=跟进日)]
    [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:DD / MM / YYYY})]
    [数据类型(DataType.Date)
    公众的DateTime? FollowUpDate {搞定;组; }
}公共类IncidentManager
{
    公共ContactIncident事件{搞定;组; }
    公共字符串测试{搞定;组; }
}
公众的ActionResult创建(INT?ID)
{
    IncidentManager IM =新IncidentManager();
    ContactIncident CI =新ContactIncident();
    ci.IncidentDateTime = DateTime.Now;
    ci.FollowUpDate = DateTime.Now.AddDays(14);
    im.Incident = Cl;
    返回查看(IM);
}[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult创建(IncidentManager IM)
{
    如果(ModelState.IsValid)
    {
        ContactIncident CI =新ContactIncident();
        ci.IncidentDateTime = incident.Incident.IncidentDateTime;
        ci.Description = im.Incident.Description;
        返回RedirectToAction(「指数」);
    }
    返回查看(事件);
}

查看:

  @model MyApp.Web.ViewModels.IncidentManager
@ {
    ViewBag.Title =编辑事件;
}
< H4> @ ViewBag.Title< / H4>
@using(Html.BeginForm())
{
    @ Html.AntiForgeryToken()
    < D​​IV CLASS =的形式,水平井>
        @ Html.ValidationSummary(真)
        @ Html.EditorFor(型号=> model.Test)
        < D​​IV CLASS =行>
            < D​​IV CLASS =COL-MD-2>
                @ Html.LabelFor(型号=> model.Incident.IncidentDateTime)
            < / DIV>
            < D​​IV CLASS =COL-MD-2>
                @ Html.DisplayFor(型号=> model.Incident.IncidentDateTime)
            < / DIV>
        < / DIV>
        < D​​IV CLASS =行>
            < D​​IV CLASS =COL-MD-2>
                @ Html.LabelFor(型号=> model.Incident.Description)
            < / DIV>
            < D​​IV CLASS =COL-MD-10>
                @ Html.EditorFor(型号=> model.Incident.Description,新{htmlAttributes = {新@class =表格控,行=10}})
            < / DIV>
                   < D​​IV CLASS =COL-MD-2>
                @ Html.LabelFor(型号=> model.Incident.FollowUpDate)
            < / DIV>
            < D​​IV CLASS =COL-MD-2>
                @ Html.EditorFor(型号=> model.Incident.FollowUpDate,新{htmlAttributes = {新@class =表格控},})
            < / DIV>
        < / DIV>
    < / DIV>
    < D​​IV CLASS =表单组>
        < D​​IV CLASS =COL-MD-偏移2 COL-MD-10>
            <输入类型=提交值=保存级=BTN BTN-默认的/>
        < / DIV>
    < / DIV>
}


解决方案

的问题是,的 DefaultModelBinder 将不能,如果你使用不同的参数名称嵌套模型正确映射。您必须使用相同的参数名称作为视图模型的名称。

 公众的ActionResult创建(IncidentManager incidentManager)

作为一般的做法,始终使用名称的模型作为参数名称,以避免映射问题。

更新:

该DefaultModelBinder使用公约为本的映射。

  IncidentManager.Incident = incidentManager.Incident(将映射)
IncidentManager.Incident = im.Incident(不会因为地图'聊天室'!='incidentManager')

I have a complex view model that I am passing to a create view. When I enter data on the page and post it the model is empty. Both the fields in the sub-object and the "test" field are empty. Why?

public class ContactIncident
{
    [Key]
    public int Id { get; set; }

    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Display(Name = "Incident Date")]
    [DataType(DataType.Date)]
    public DateTime? IncidentDateTime { get; set; }

    [Display(Name = "Follow Up Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    [DataType(DataType.Date)]
    public DateTime? FollowUpDate { get; set; }
}

public class IncidentManager
{    
    public ContactIncident Incident { get; set; }
    public string Test { get; set; }
}


public ActionResult Create(int? id)
{
    IncidentManager im = new IncidentManager();
    ContactIncident ci = new ContactIncident();
    ci.IncidentDateTime = DateTime.Now;
    ci.FollowUpDate = DateTime.Now.AddDays(14);
    im.Incident = ci;
    return View(im);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IncidentManager im)
{
    if (ModelState.IsValid)
    {
        ContactIncident ci = new ContactIncident();
        ci.IncidentDateTime = incident.Incident.IncidentDateTime;
        ci.Description = im.Incident.Description;
        return RedirectToAction("Index");
    }
    return View(incident);
}

View:

@model MyApp.Web.ViewModels.IncidentManager
@{
    ViewBag.Title = "Edit Incident";
}
<h4>@ViewBag.Title</h4>    
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal well">
        @Html.ValidationSummary(true)      
        @Html.EditorFor(model=>model.Test)   
        <div class="row">
            <div class="col-md-2">
                @Html.LabelFor(model => model.Incident.IncidentDateTime)
            </div>
            <div class="col-md-2">
                @Html.DisplayFor(model => model.Incident.IncidentDateTime)
            </div>
        </div>
        <div class="row">
            <div class="col-md-2">
                @Html.LabelFor(model => model.Incident.Description)
            </div>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Incident.Description, new { htmlAttributes = new { @class = "form-control", rows = "5" }, })
            </div>
                   <div class="col-md-2">
                @Html.LabelFor(model => model.Incident.FollowUpDate)
            </div>
            <div class="col-md-2">
                @Html.EditorFor(model => model.Incident.FollowUpDate, new { htmlAttributes = new { @class = "form-control"}, })
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
}

解决方案

The problem is that the DefaultModelBinder won't be able to map nested models properly if you use a different parameter name. You must use the same parameter name as the ViewModel name.

public ActionResult Create(IncidentManager incidentManager)

As a general practice, always use the name of the model as the parameter name to avoid mapping problems.

UPDATE:

The DefaultModelBinder uses "convention based" mapping.

IncidentManager.Incident = incidentManager.Incident (will map)
IncidentManager.Incident = im.Incident (won't map because 'im' != 'incidentManager')

这篇关于asp.Net MVC视图模型是空的后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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