1

My question is about binding in MVC. Could you please help me with it?

My controller:

public class TestController : Controller
 {
        //
        // GET: /Test/
        [HttpGet]
        public ActionResult Index()
        {
            return View(new TestModel());
        }
        public ActionResult Index(TestModel test)
        {
           return View(test);
        }
 }

My View:

@model MvcApplication1.Models.TestModel

@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.test) // or x.Test
<input type="submit" value="Set"/>
}

My model:

public class TestModel
{
public string test { get; set; } // or Test{get;set;}
}

The problem is connected with the name of parameter "test" in controller as I understand. I have just changed it to "model" and the binding is working. But it is not working in original state (the name of parameter is 'test'), 'test' parameter is null.

Please give me understand why the binding is not working in current example. Thank you a lot!

2
  • 1
    The value of the test parameter in the model is null because it is never being set to anything. Commented Feb 1, 2013 at 14:15
  • I don't understand- why. My post body has test=value. First, the binder is searching 'test.test' in post body (and some another places). After it should search simple 'test' and is binding to model. Right ? Or, I think wrong about binding process.. Commented Feb 1, 2013 at 14:31

1 Answer 1

0

You need an [HttpPost] attribute on your second method. You also cannot use test as your variable name, as it is the same name as the property of the class you're attempting to bind to; the ModelBinder fails to determine which to use. Your code would look as follows:

public class TestController : Controller
 {
        //
        // GET: /Test/
        [HttpGet]
        public ActionResult Index()
        {
            return View(new TestModel());
        }

        //
        // POST: /Test/
        [HttpPost]
        public ActionResult Index(TestModel testModel)
        {
           return View(testModel);
        }
 }
Sign up to request clarification or add additional context in comments.

6 Comments

It is not working. The test variable in action "Index(TestModel test)" is null. The default binding still is not working .
Are you doing anything to modify the ModelBindingContext?
No it is just the code that I have added to Empty MVC4 New project. I added these controller, view and model and nothing else. Maybe it is some specific implementation of default binding, i don't know.
D'oh! I just realized the name of your property in your class is the same as the name of your variable in your Index method. See the answer to this question: stackoverflow.com/questions/7508896/…
yep, I understand it. I have read about default binding in some books, but i can't found some notes about it. Could you provide some references about it ? As I know, first - binder is searching test.test in some places (in post, query string, default values in routes and so on), second - is should just search - 'test' and bind it to my 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.