在MVC 4模型提交表单是控制器POST方法无效 [英] Submitting Form in MVC 4 model is null in controller post method

查看:151
本文介绍了在MVC 4模型提交表单是控制器POST方法无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题,现在的问题是,我不能让我的模式进入我的控制器,当我提交这份以下表格。我想有从开票codeS(即开票codeObjects列表)遍历和显示的项目。我已删除了这些不在的情况确实有些相关code,使其更短,更易于阅读。

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 list of BillingCodeObjects) loop through and display. I have removed some code from these that isn't really relevant to the situation to make it shorter and easier to read.

下面是code对我的看法...

Here is the code for my view...

@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>

和这里是一些code为我的控制器:

And here is some code for my controller:

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

}
}

和最后,这里要说的是在模型的信息:

and lastly, here is the info that is in the model:

    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()
        {

        }
    }

这里

是调试当地人的图片返回表单时..

here is a picture of the debug locals when the form is returned..

当地人的图片

推荐答案

您正在做的意见的foreach,所以输入元素应该有张贴的地方像这个值:
NAME =结算codeS [0] .EnterTimeHours NAME =结算codeS [0] .EnterTimeMinutes您可以在网络选项卡上的检查Chrome开发者工具(CTRL + SHIFT + C)的要求
或者仅仅查看源。如果是这种情况,您要提交多个开票codeObj对象。你必须有一个接收的控制器。

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.

看看源$ C ​​$ C,因为这可以极大地帮助你了解这是怎么回事幕后。

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

试试这个控制器上:

[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.

后的意见和更多code提供
改变你的看法为:

after comments and more code provided change your view to :

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

创建EditorTemplates /计费codeObj.cshtml新CSHTML:

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>

这篇关于在MVC 4模型提交表单是控制器POST方法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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