[英]ASP.NET MVC 4 form post variables are null in receiving controller
[英]Controller receiving null values from a form ASP.NET MVC5
我有兩個來自市場及其分支機構的下拉菜單,所以我在選擇市場時制作了它,它在分支機構下拉菜單中加載了分支機構
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>
還有那個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>")
});
});
});
一切正常,但是當我發布整個表格時
@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>
並且模型類
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}");
}
}
}
在調試時,我可以看到指向模型類的點並將分支設置為空值我很困惑謝謝
Branch 是一個內置模型綁定器一無所知的類,您必須編寫一個自定義模型綁定器,將 Branch 發布的值轉換為Branch
實例或將模型中的Branch
轉換為string
或int
:
public string Branch { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.