2014-07-24 7 views
5

पर खाली है, मेरे पास एक जटिल दृश्य मॉडल है जिसे मैं एक व्यू व्यू में भेज रहा हूं। जब मैं पृष्ठ पर डेटा दर्ज करता हूं और इसे पोस्ट करता हूं तो मॉडल खाली होता है। उप-वस्तु और "परीक्षण" फ़ील्ड दोनों फ़ील्ड खाली हैं। क्यूं कर?asp.Net एमवीसी व्यू मॉडल

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

दृश्य:

@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

क्या आप अपना सीएचटीएमएल दिखा सकते हैं? देखना चाहते हैं कि आप फॉर्म तत्वों को कैसे परिभाषित कर रहे हैं –

+0

मुझे लगता है कि 'सीआई.इंसेन्टडेट टाइमटाइम = घटना। इंस्पेन्टेंट। इंटिडेंटडेट टाइम;' एक टाइपो है और इसकी वास्तव में 'सीआई.इंसेन्टडेट टाइमटाइम = im.Incident.IncidentDateTime;'। 'रिटर्न व्यू (घटना) के लिए डिट्टो;' रिटर्न व्यू (आईएम); '(आपका कोड संकलित नहीं है)। अन्यथा आपका कोड ठीक काम करता है हालांकि 'IncidentDateTime'' शून्य 'होगा क्योंकि आप इसके लिए कोई फॉर्म इनपुट नहीं बनाते हैं। –

उत्तर

4

समस्या यह है कि DefaultModelBinder नेस्ट मॉडल ठीक से मैप करने के लिए करता है, तो आप एक अलग पैरामीटर नाम का उपयोग नहीं कर सकेंगे है। आपको ViewModel नाम के रूप में उसी पैरामीटर नाम का उपयोग करना होगा।

public ActionResult Create(IncidentManager incidentManager) 

सामान्य अभ्यास के रूप में, मैपिंग समस्याओं से बचने के लिए हमेशा पैरामीटर नाम के रूप में मॉडल के नाम का उपयोग करें।

अद्यतन:

DefaultModelBinder उपयोग करता है "सम्मेलन आधारित" मानचित्रण।

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

कृपया विस्तृत करें। क्या आप कक्षा घटना प्रबंधक से बात कर रहे हैं? यदि ऐसा है तो क्यों? –

+0

@ जॉन्स मैंने अपना जवाब अपडेट किया है। – Yorro

+0

मैंने बदलाव किया और यह काम किया। मैंने पहले कई बार इसमें भाग लिया है और हमेशा इसके आसपास काम किया है, लेकिन यह बहुत बेहतर होगा। –

संबंधित मुद्दे