Submitting Form in MVC 4 model is null in controller post method

前端 未结 3 1977
粉色の甜心
粉色の甜心 2021-01-02 09:42

So my problem right now is that I cant get my model into my controller when I submit this following form. I am trying to have the items from the BillingCodes (which is a lis

相关标签:
3条回答
  • 2021-01-02 10:06

    Your View is type of BillingCodeList, but you are trying to send List<BillingCodeObj> to you action in controller.

    Try to change action as follows:

    [HttpPost]
        public void SubmitTimesheet(BillingCodeList model)
        {
    
            string uri = "";
    
            foreach (var billingCode in model.BillingCodes)
            {
               //do stuff with each of these
            }
        }
    
    0 讨论(0)
  • 2021-01-02 10:20

    You are doing a foreach on the views, so the input elements should have the values posted somewhere like to this : name="BillingCodes[0].EnterTimeHours", name="BillingCodes[0].EnterTimeMinutes" you can inspect in the network tab the request on Chrome Developer Tools (CTRL+SHIFT+C) or just viewing the source. If this is the case, you are submitting multiple BillingCodeObj objects. You must have a controller that receive that.

    Take a look at the source code as this can greatly help you understand what's going on behind the scenes.

    Try this on your controller:

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

    You can also (for debugging purposes) do

    public void SubmitTimesheet(FormCollection form){}
    

    and inspect the form how is populated on debug.

    after comments and more code provided change your view to :

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

    create a new cshtml in 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 讨论(0)
  • 2021-01-02 10:28

    edit: It doesn't seem that you're initializing your BillingCodeList's list element to a new List<BillingCode>() before you send the model off to the view. Or are you?

    0 讨论(0)
提交回复
热议问题