2014-07-24 19 views
5

Oluşturma görünümüne aktardığım karmaşık bir görünüm modelim var. Sayfadaki veriyi girip postaladığımda model boş. Alt nesne ve "test" alanındaki alanlar boştur. Niye ya?asp.Net MVC görünüm modeli gönderide boş

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); 
} 

Görünüm:

@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> 
} 
+1

cshtml'nizi gösterebilir misiniz? –

+0

form öğelerini nasıl tanımladığınızı görmek istiyorum. I c.IncidentDateTime = incident.Incident.IncidentDateTime; 'bir yazım hatası ve gerçekten 'ci.IncidentDateTime = im.Incident.IncidentDateTime;'. Geri Dönme Görünümü (olay); 'Geri Dönme Görünümü (im);' (kodunuz derleme değil). Aksi halde kodunuz, incidentDateTime 'null' olmasına rağmen iyi çalışır, çünkü bunun için bir form girdisi yaratmazsınız. –

cevap

4

sorun DefaultModelBinder farklı bir parametre adı kullanmak düzgün iç içe modelleri haritaya mümkün olmayacaktır olmasıdır. ViewModel adıyla aynı parametre adı'u kullanmanız gerekir. Genel bir uygulama olarak, eşleme sorunlarını önlemek için her zaman modelin adını parametre adı olarak kullanın.

GÜNCELLEME:

DefaultModelBinder kullandığı "kongre esaslı" haritalaması.

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

Lütfen detaylandırın. IncidentManager sınıfına mı konuşuyorsunuz? Öyleyse neden? –

+0

@JohnS Cevabımı güncelledim. – Yorro

+0

Değişikliği yaptım ve işe yaradı. Bu birkaç kez daha önce karşılaştım ve her zaman bunun etrafında çalıştı, ama bu daha iyi olacak. –

İlgili konular