0

So my situation is that I am populating a model based on a text box in my view. My code is this

View:

@model LiteModel
@using (Html.BeginForm())
{
    <label>Lite ID</label>
    @Html.TextBoxFor(model => model.LiteId)
    <br />
    <label>Width</label>
    @Html.TextBoxFor(model => model.BaseSize);
    <br />
    <label>Height</label>
    @Html.TextBoxFor(model => model.HeightSize);
    <br />
    <input type="submit" class="right" />
    <br />
}

Controller:

    [HttpPost]
    public IActionResult Index([FromForm]LiteModel model)
    {
        model = LoadLiteInfo(model);

        return View(model);
    }

Model:

namespace LiteViewer.Models
{
    public class LiteModel
    {
        public string LiteId { get; set; }

        public string ShapeId { get; set; }

        public double BaseSize { get; set; }

        public double HeightSize { get; set; }
    }
}

Now I know the LoadLiteInfo method works as its supposed to, it populates the model object just as it should. For some reason however the changes in the model are not being reflected in the view. I cant for the life of my figure out why its not working.

1 Answer 1

1

If you are making changes to the posted model you need to call ModelState.Clear().

Sign up to request clarification or add additional context in comments.

2 Comments

So i tried just using a new model as your code sample suggested. However it didnt make any difference. But once I tried the ModelState.Clear() worked like a charm.
OK, I will edit my answer to remove the bit about constructing a new model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.