首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.NET MVC post模型始终为空

ASP.NET MVC post模型始终为空
EN

Stack Overflow用户
提问于 2018-08-27 01:23:15
回答 1查看 1.5K关注 0票数 1

我有一个正在构建的ASP.NET MVC应用程序,但我不明白为什么回发的模型总是空的。

cshtml:

代码语言:javascript
复制
<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>

我已经查看了表单,在发布时,所有字段都具有正确的值。当它到达控制器时,它总是为空。有没有人能帮我解决这个问题。

模型类:

代码语言:javascript
复制
public class TotalItemValue:IEntity
{
    public int Id { get; set; }

    public int? TotalItemCategoryId { get; set; }

    [StringLength(125)]
    public string Label { get; set; }

    public decimal? Value { get; set; }
    public int? Quantity { get; set; }
    public int QuoteId { get; set; }
    public int? TotalTemplateId { get; set; }

    public bool AllowDataEntry { get; set; }
    public bool IsMiscellaneous { get; set; }

    public TotalItemCategory TotalItemCategory { get; set; }
    public TotalTemplate TotalTemplate { get; set; }
}

控制器:

代码语言:javascript
复制
public ActionResult CreateMiscellaneousItem(int quoteId)
{
    TotalItemValue totalItemValue = _expressionTotalService.CreateMiscellaneousItem(quoteId);
    return View(totalItemValue);
}

[HttpPost]
public ActionResult CreateMiscellaneousItem( TotalItemValue value)
{
    _expressionTotalService.SaveMiscellaneousItem(value);

    return RedirectToAction("Totals", new{quoteId=value.QuoteId});
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-27 01:32:04

因为您的模型包含名为value value 的属性,并且您还在POST方法中将模型的参数命名为,所以将其命名为null。

您必须在TotalItemValue模型中添加QuoteId属性,并将请求参数名称从value更改为model

试试这个:

代码语言:javascript
复制
public class TotalItemValue:IEntity
{
      ...
      public int QuoteId{ get; set; }
      ...
}


[HttpPost]
    public ActionResult CreateMiscellaneousItem(TotalItemValue model)
    {
       ...
       _expressionTotalService.SaveMiscellaneousItem(model);

       return RedirectToAction("Totals", new{model.QuoteId});
    }

视图

代码语言:javascript
复制
@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>
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52028631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档