简体   繁体   English

控制器从表单 ASP.NET MVC5 接收空值

[英]Controller receiving null values from a form ASP.NET MVC5

I have two dropdownmenu from markets and their branches so i made it when choose a market, it load its branches in branches dropdownmenu我有两个来自市场及其分支机构的下拉菜单,所以我在选择市场时制作了它,它在分支机构下拉菜单中加载了分支机构

The html design html 设计

<div style="width : 50%">
        <div class="form-group" style="display:inline-block">
            @Html.LabelFor(m => m.Market)
            @Html.DropDownListFor(m => m.Market, new SelectList(Model.Markets, "Id", "Name"), "Select Market", new { @class = "form-control" })
        </div>
        <div class="form-group" style="float:right">
            @Html.LabelFor(m => m.Branch)
            @Html.DropDownListFor(m => m.Branch, new SelectList("", "Id", "Name"), "Select Branch", new { @class = "form-control" })
        </div>
    </div>

and that ajax to load branches还有那个ajax来加载分支

 $(function () {
    $("#Market").on("change", function () {
        $.get("/Home/GetBranches", { id: $("#Market").val() }, function (data) {
            $("#Branch").empty();
            console.log(data);
            $.each(data, function (index, row) {
                $("#Branch").append("<option value='" + row.Id + "'>" + row.Name + "</option>")
            });
        });
    });

and everything working well but when i post the whole form一切正常,但是当我发布整个表格时

   @using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

    <div style="width : 50%">
        <div class="form-group" style="display:inline-block">
            @Html.LabelFor(m => m.Market)
            @Html.DropDownListFor(m => m.Market, new SelectList(Model.Markets, "Id", "Name"), "Select Market", new { @class = "form-control" })
        </div>
        <div class="form-group" style="float:right">
            @Html.LabelFor(m => m.Branch)
            @Html.DropDownListFor(m => m.Branch, new SelectList("", "Id", "Name"), "Select Branch", new { @class = "form-control" })
        </div>
    </div>

    <input class='datepicker' />

    <div style="width: 50%">
        <div class="form-group" style="display:inline-block">
            @Html.LabelFor(m => m.Date)
            @Html.TextBoxFor((m => m.Date), new { @class = "form-control form-inline datepicker", placeholder = "eg 1 Jan 2018" })
            @Html.ValidationMessageFor(m => m.Date)
        </div>
        <div class="form-group" style="float:right">
            @Html.LabelFor(m => m.Time)
            @Html.TextBoxFor(m => m.Time, new { @class = "form-control form-inline", placeholder = "hh:mm" })
            @Html.ValidationMessageFor(m => m.Time)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.CustName)
        @Html.TextBoxFor(m => m.CustName, new { @class = "form-control fouc" })
        @Html.ValidationMessageFor(m => m.CustName)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.OrderNumber)
        @Html.TextBoxFor(m => m.OrderNumber, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.OrderNumber)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Total)
        @Html.EditorFor(m => m.Total, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(m => m.Total)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.CouponValue)
        @Html.EditorFor(m => m.CouponValue, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(m => m.CouponValue)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Notes)
        @Html.TextBoxFor(m => m.Notes, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Notes)
    </div>
    <button type="submit" class="btn btn-primary btn-lg">Save</button>

i receive the branch null value我收到分支空值在此处输入图片说明

when i open the network in browser, i can see values passed当我在浏览器中打开网络时,我可以看到传递的值在此处输入图片说明

and that the Model Class并且模型类

    using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Takers.Models;

namespace Takers.ViewModel
{
    public class OrderView
    {
        [Required]
        public string OrderNumber { get; set; }

        [Required]
        public string CustName { get; set; }

        [Required]
        public decimal CouponValue { get; set; }

        [Required]
        public string Notes { get; set; }

        [Required]
        public string UserName { get; set; }

        [Required]
        public decimal Total { get; set; }

        public string Time { get; set; }

        public string Date { get; set; }

        public Market Market { get; set; }

        public Branch Branch { get; set; }

        public ICollection<Market> Markets { get; set; }

        public ICollection<Branch> Branches { get; set; }

        public DateTime GetDateTime()
        {
            return DateTime.Parse($"{Date} {Time}");
        }
    }
}

while debuging i can see the point going to the model class and set the branch with null value I'm so confused Thank You在调试时,我可以看到指向模型类的点并将分支设置为空值我很困惑谢谢

Branch 是一个内置模型绑定器一无所知的类,您必须编写一个自定义模型绑定器,将 Branch 发布的值转换为Branch实例或将模型中的Branch转换为stringint

public string Branch { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM