2

I have a form that I cannot get to bind values to my model. The model is empty on the post action.

I've stripped everything down to the simplest of examples and still can't get it to bind.

The model

public class simplemodel
{
    public string SomeVal;
}

The view

@model MyApp.Models.simplemodel

@using(Html.BeginForm("Index", "Test", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.SomeVal);
    <input type="submit" value="submit" id="btnSubmit" />
}

The controller action

[HttpPost]
public ActionResult Index(simplemodel model)
{
    string ReceivedVal = model.SomeVal;

    //do whatever...
}

This is as straight forward as it gets, but my model doesn't bind.

ReceivedVal is always empty. I have plenty of other places where I'm binding to models with no issue. I'm baffled. What could I possibly be missing?

I do receive "SomeVal" if I change the controller action to receive a FormCollection. So I know it's getting posted properly, it just won't bind to the model.

2
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. Commented Feb 25, 2017 at 2:27
  • May you can provide your raw html Commented Feb 25, 2017 at 17:42

1 Answer 1

4

I had all but given up searching... and was just reading some of the answers that weren't chosen to similar problems and learned that my model must have properties, not just public fields. I must have gotten lazy and didn't add the { get; set; }

I never realized it was necessary!

Sorry!

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

Comments

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.