我有一个正在构建的ASP.NET MVC应用程序,我无法弄清楚为什么回发的模型始终为空。
cshtml:
$("#list li a:not(li:nth-child(2) a)").removeClass("active");
我已经查看了表单,发布时所有字段的值都正确。仅当到达控制器时,它始终为null。有人可以帮我解决这个问题吗?
模型类:
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<style>
.active {
color: red;
}
</style>
<div id="list">
<ul>
<li><a class="active" href="#">One</a></li>
<li><a class="active" href="#">Two</a></li>
<li><a class="active" href="#">Three</a></li>
</ul>
</div>
控制器:
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.HiddenFor(model => model.TotalItemCategoryId, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Label, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Label, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Label, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Value, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Value, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Value, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.HiddenFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.QuoteId, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.TotalTemplateId, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group">
@Html.HiddenFor(model => model.AllowDataEntry,new{@readonly="readonly"})
</div>
<div class="form-group">
@Html.HiddenFor(model => model.IsMiscellaneous)
</div>
答案 0 :(得分:7)
它为null,因为您的模型包含名为value
的属性,并且您还在POST方法中为模型value
命名了参数。
您必须在QuoteId
模型中添加TotalItemValue
属性,然后将请求参数名称从value
更改为model
。
尝试一下:
public class TotalItemValue:IEntity
{
...
public int QuoteId{ get; set; }
...
}
[HttpPost]
public ActionResult CreateMiscellaneousItem(TotalItemValue model)
{
...
_expressionTotalService.SaveMiscellaneousItem(model);
return RedirectToAction("Totals", new{model.QuoteId});
}
查看
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>TotalItemValue</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TotalItemCategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TotalItemCategoryId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TotalItemCategoryId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Label, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Label, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Label, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Value, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Value, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Value, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QuoteId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QuoteId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QuoteId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TotalTemplateId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TotalTemplateId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TotalTemplateId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AllowDataEntry, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.AllowDataEntry)
@Html.ValidationMessageFor(model => model.AllowDataEntry, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsMiscellaneous, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsMiscellaneous)
@Html.ValidationMessageFor(model => model.IsMiscellaneous, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}