2

I have an ASP.NET MVC app that I'm building and I cannot figure out why the model being posted back is always null.

cshtml:

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

I have looked at the form and all of the fields are there with the correct values when posting. just when it gets to the controller it is always null. Can someone please help me figure this out.

Model class:

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

Controller:

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});
}
3
  • 1
    Can you show the controller code please. Commented Aug 26, 2018 at 17:27
  • 1
    Have you tried to add [FromBody] attribute for your model ? Commented Aug 26, 2018 at 17:44
  • 1
    There's no BeginForm() in your cshtml. Is that just an oversight or is it really not there? Commented Aug 26, 2018 at 17:44

1 Answer 1

9

Its null because your model contains a property named value and you have also named the parameter for your model value in the POST method.

You must add QuoteId Property in TotalItemValue model and just change request parameter name from value to model.

Try This :

public class TotalItemValue:IEntity
{
      ...
      public int QuoteId{ get; set; }
      ...
}


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

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

View

@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>
}
Sign up to request clarification or add additional context in comments.

12 Comments

it is. TotalItemValue value)
I think the issue is you are passing in int quoteId and TotalItemValue value as the params for your Post, you need to change it to be just TotalItemValue, otherwise youll need to implement a custom model binder (i think) to do 2 params like that to your Post action. If you need quoteId, add it to your model which it looks like it is already there.
I do this in many different places in the application. This should work, quoteId is passed in from url
@JamTay317 Put quoteId in model.
I have changed to what you asked, its still null.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.