【问题标题】:Submitting Form in MVC 4 model is null in controller post method在 MVC 4 模型中提交表单在控制器发布方法中为空
【发布时间】:2013-08-14 15:25:36
【问题描述】:

所以我现在的问题是,当我提交以下表单时,我无法将模型放入控制器中。我试图让 BillingCodes(这是 BillingCodeObjects 列表)中的项目循环并显示。我已经从这些代码中删除了一些与实际情况无关的代码,以使其更短且更易于阅读。

这是我的观点的代码......

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

这是我的控制器的一些代码:

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

}
}

最后,这是模型中的信息:

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

        }
    }

这是返回表单时调试本地的图片..

image of locals

【问题讨论】:

    标签: c# asp.net-mvc forms asp.net-mvc-4 razor


    【解决方案1】:

    您正在对视图进行 foreach,因此输入元素应将值张贴在如下所示的某处: name="BillingCodes[0].EnterTimeHours", name="BillingCodes[0].EnterTimeMinutes" 您可以在网络选项卡中检查 Chrome 开发人员工具 (CTRL+SHIFT+C) 上的请求 或者只是查看源代码。如果是这种情况,您将提交多个 BillingCodeObj 对象。你必须有一个控制器来接收它。

    查看源代码,因为这可以极大地帮助您了解幕后发生的事情。

    在你的控制器上试试这个:

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

    你也可以(出于调试目的)这样做

    public void SubmitTimesheet(FormCollection form){}
    

    并检查表单在调试时是如何填充的。

    在 cmets 和提供更多代码之后 将您的视图更改为:

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

    在 EditorTemplates/BillingCodeObj.cshtml 中创建一个新的 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>
    

    【讨论】:

    • 好的,谢谢。第一件事没有奏效,billingCodes 对象仍然为空。所以我会花一些时间检查它返回的表单。
    • 如果您可以在 html 上发布生成的输入,我将很乐意为您提供帮助。
    • 好的,我编辑了我的原始帖子,并在此处添加了一张当地人的照片:link
    • 据我所知,mvc 正在解释你不是在发送一个集合,而是一个集合。 public void SubmitTimesheet(BillingCodeObj item){
    • 是的,你是对的,它确实有效。因此,如果我一次输入多个帐单代码的信息,尽管它只提交其中一个..
    【解决方案2】:

    您的视图类型为 BillingCodeList,但您正尝试将 List&lt;BillingCodeObj&gt; 发送给您在控制器中的操作。

    尝试如下改变动作:

    [HttpPost]
        public void SubmitTimesheet(BillingCodeList model)
        {
    
            string uri = "";
    
            foreach (var billingCode in model.BillingCodes)
            {
               //do stuff with each of these
            }
        }
    

    【讨论】:

    • 嗯。我认为您是对的,但是我返回的模型对象中的 billingCodes 列表仍然为空..有什么想法吗?我将编辑我的问题以包含您的代码。
    【解决方案3】:

    编辑:在将模型发送到视图之前,您似乎没有将 BillingCodeList 的列表元素初始化为 new List&lt;BillingCode&gt;()。还是你?

    【讨论】:

    • 我在做:BillingCodeList billingCodeList = JsonConvert.DeserializeObject(result);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多