2013-08-14 12 views
8

Tak więc moim problemem jest to, że nie mogę pobrać mojego modelu do kontrolera, gdy przesyłam poniższy formularz. Próbuję przechodzić i wyświetlać elementy z listy BillingCodes (która jest listą BillingCodeObjects). Usunąłem z nich kod, który nie jest tak naprawdę związany z sytuacją, aby był krótszy i łatwiejszy do odczytania.Przesyłanie formularza w modelu MVC 4 ma wartość zerową w metodzie posta kontrolera

Oto kod na moim zdaniem ...

@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post)) 
{ 


foreach (var item in Model.BillingCodes) 
{ 

    <div class="button-padding"> 
     <a class="btn btn-large btn-danger btn-block billCodeBtn"> 
      <div class="btnText">@item.Name</div> 
      <div class="btnTime">@item.TotalHours</div> 

      <i class="icon-chevron-down billCodeIconUp billCodeIcon"></i> 
      <i class="hidden icon-chevron-up billCodeIconDown billCodeIcon"></i> 
     </a> 

     <div class="content" > 
      <div class="row timeEntry"> 
       <p></p> 
       <div class="col-12 col-lg-2"> 
        Enter Time: 
     <div class="row"> 
      <div class="col-12"> 
       @Html.DropDownListFor(model => item.EnterTimeHours, new SelectList(new[] { 
        new { Value = "0", Text = "0" }, 
        new { Value = "1", Text = "1" }, 
        new { Value = "2", Text = "2" }, 
        new { Value = "3", Text = "3" }, 
        new { Value = "4", Text = "4" }, 
        new { Value = "5", Text = "5" }, 
        new { Value = "6", Text = "6" }, 
        new { Value = "7", Text = "7" }, 
        new { Value = "8", Text = "8" }, 
        new { Value = "9", Text = "9" }, 
        new { Value = "10", Text = "10" }, 
        new { Value = "11", Text = "11" }, 
        new { Value = "12", Text = "12" }, 
       }, "Value", "Text")) <b>:</b> 
       @Html.DropDownListFor(model => item.EnterTimeMinutes, new SelectList(new[] { 
        new { Value = "0", Text = "00" }, 
        new { Value = "15", Text = "15" }, 
        new { Value = "30", Text = "30" }, 
        new { Value = "45", Text = "45" }, 
       }, "Value", "Text")) 

      </div> 
     </div> 
       </div> 
       <div class="col-lg-2"></div> 

       <div class="col-lg-1"></div> 
       <div class="control-group col-12 col-lg-2"> 
        <label class="checkbox"> 
         Billable @Html.CheckBoxFor(model => item.Billable) 
        </label> 
       </div> 
       <div class="col-12 col-lg-2"> 
        Enter Memo: 
     <div class="row"> 
      <div class="col-12"> 
       @Html.TextAreaFor(model => item.Comment) 
      </div> 
     </div> 

I tu jest jakiś kod dla mojego kontrolera:

public class TimesheetController : Controller 
{ 
    // 
    // GET: /Timesheet/ 

    public ActionResult Index() 
    { 

     string getBillingCodeUrl =""; 

     //SOME CODE REMOVED FOR LENGTH/READABILITY 

     foreach (var entryItem in timePeriod.TimeEntries[0].EntryCollection) 
     { 
      foreach (var billingItem in billingCodeList.BillingCodes) 
      { 
       if (entryItem.BillingCode == billingItem.Name) 
       { 
        //update record in billingItem with data from entryItem 
        billingItem.Comment = entryItem.Comment; 
        billingItem.Billable = entryItem.Billable; 
        billingItem.TotalHours = entryItem.Hours; 
       } 
      } 
     } 

     return View(billingCodeList); 
    } 
    [HttpPost] 
    public void SubmitTimesheet(BillingCodeList model) 
    { 

     string uri = ""; 

     foreach (var billingCode in model.BillingCodes) 
     { 
      //do stuff with each of these 
     } 
    } 

} 
} 

i wreszcie tu jest info, że jest w modelu:

public class BillingCodeList 
    { 
     public List<BillingCodeObj> BillingCodes; 
    } 

    public class BillingCodeObj 
    { 
     public string Name { get; set; } 
     public decimal TotalHours { get; set; } 

     public decimal EnterTimeHours { get; set; } 
     public decimal EnterTimeMinutes { get; set; } 
     public bool Billable { get; set; } 
     public string Comment { get; set; } 

     public BillingCodeObj(string name, decimal hours) 
     { 
      this.Name = name; 
      this.TotalHours = hours; 
     } 
     public BillingCodeObj() 
     { 

     } 
    } 

tutaj jest zdjęcie z mieszkańcami podczas debugowania jest zwracany formularz ..

image of locals

Odpowiedz

6

Robicie foreach na poglądy, więc elementy wejściowe powinny mieć wartości pisał gdzieś podobny do tego: name="BillingCodes[0].EnterTimeHours", name="BillingCodes[0].EnterTimeMinutes" można sprawdzić w zakładce sieci prośba o Chrome Narzędzia dla programistów (Ctrl + Shift + C) lub po prostu wyświetl źródło. W takim przypadku przesyłasz wiele obiektów BillingCodeObj. Musisz mieć kontroler, który to odbierze.

Spójrz na kod źródłowy, ponieważ może to znacznie pomóc zrozumieć, co dzieje się za kulisami.

Spróbuj na kontrolerze:

[HttpPost] 
public void SubmitTimesheet(IEnumerable<BillingCodeObj> billingCodes){ 
} 

Można też (dla celów debugowania) zrobić

public void SubmitTimesheet(FormCollection form){} 

i sprawdzić formę jak jest wypełniona na debugowania.

po komentarze i więcej kodu pod warunkiem zmienić widok na:

@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post)) 
{ 
    @Html.EditorFor(m=>m.BillingCodes) 
} 

utworzyć nowy cshtml w EditorTemplates/BillingCodeObj.cshtml:

@model BillingCodeObj 
<div class="button-padding"> 
    <a class="btn btn-large btn-danger btn-block billCodeBtn"> 
     <div class="btnText">@Model.Name</div> 
     <div class="btnTime">@Model.TotalHours</div> 

     <i class="icon-chevron-down billCodeIconUp billCodeIcon"></i> 
     <i class="hidden icon-chevron-up billCodeIconDown billCodeIcon"></i> 
    </a> 

    <div class="content" > 
     <div class="row timeEntry"> 
      <p></p> 
      <div class="col-12 col-lg-2"> 
       Enter Time: 
    <div class="row"> 
     <div class="col-12"> 
      @Html.DropDownListFor(model => model.EnterTimeHours, new SelectList(new[] { 
       new { Value = "0", Text = "0" }, 
       new { Value = "1", Text = "1" }, 
       new { Value = "2", Text = "2" }, 
       new { Value = "3", Text = "3" }, 
       new { Value = "4", Text = "4" }, 
       new { Value = "5", Text = "5" }, 
       new { Value = "6", Text = "6" }, 
       new { Value = "7", Text = "7" }, 
       new { Value = "8", Text = "8" }, 
       new { Value = "9", Text = "9" }, 
       new { Value = "10", Text = "10" }, 
       new { Value = "11", Text = "11" }, 
       new { Value = "12", Text = "12" }, 
      }, "Value", "Text")) <b>:</b> 
      @Html.DropDownListFor(model => model.EnterTimeMinutes, new SelectList(new[] { 
       new { Value = "0", Text = "00" }, 
       new { Value = "15", Text = "15" }, 
       new { Value = "30", Text = "30" }, 
       new { Value = "45", Text = "45" }, 
      }, "Value", "Text")) 

     </div> 
    </div> 
      </div> 
      <div class="col-lg-2"></div> 

      <div class="col-lg-1"></div> 
      <div class="control-group col-12 col-lg-2"> 
       <label class="checkbox"> 
        Billable @Html.CheckBoxFor(model => model.Billable) 
       </label> 
      </div> 
      <div class="col-12 col-lg-2"> 
       Enter Memo: 
    <div class="row"> 
     <div class="col-12"> 
      @Html.TextAreaFor(model => model.Comment) 
     </div> 
    </div> 
+0

Dobra, dzięki.Pierwsza rzecz nie zadziałała, obiekt billingCodes wciąż ma wartość null. Tak więc poświęcę trochę czasu i sprawdzam formę, którą zwraca. –

+0

jeśli możesz opublikować wygenerowane dane wejściowe na html, z przyjemnością spróbuję ci pomóc. –

+0

OK Edytowałem mój oryginalny wpis i zawierałem zdjęcie miejscowych również tutaj: [link] (http://i.imgur.com/0KXBDXr.png) –

0

Państwa zdanie jest rodzaj BillingCodeList, ale staramy się wysłać do ciebie List<BillingCodeObj> akcji w kontrolerze.

Spróbuj zmienić akcję w następujący sposób:

[HttpPost] 
    public void SubmitTimesheet(BillingCodeList model) 
    { 

     string uri = ""; 

     foreach (var billingCode in model.BillingCodes) 
     { 
      //do stuff with each of these 
     } 
    } 
+0

Hmm. Myślę, że masz rację, ale Lista kodów rozliczeniowych w obiekcie modelu, który otrzymuję, wciąż jest pusta. Jakieś pomysły? Będę edytować moje pytanie w celu uwzględnienia kodu. –

0

edit: Nie wydaje się, że jesteś inicjowanie listy elementu Twojego BillingCodeList jest do new List<BillingCode>() przed wysłaniem modelu off do widoku. A może ty?

+0

Robię: BillingCodeList billingCodeList = JsonConvert.DeserializeObject (result); –

Powiązane problemy