Al.
Al.

Reputation: 875

Model binding in Asp.net MVC

I am redirecting from default page to home page. user has been resolved successfully in the action method. But not the user in tc instance. My default page (previous page) is not strongly typed.

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index(string user, TestClass tc)
    {
        ViewBag.Name = user; // resolved successfully 
        ViewBag.TCName = tc.user; // its null
        return View();
    }

}

public class TestClass
{
    public string user;
}

** Default Page's .cshtml code piece**

<form action="Home" method="post">
        <fieldset>
            <legend>HTML 5</legend>
            <label for="user">required</label>
            <input id="user" type="text" name="user" required="required">

My question is why tc.user is null? [Isn't model binding just string to string match from post variables? if so, tc.user should have been populated as user in action method right?]

Upvotes: 0

Views: 139

Answers (1)

Gecko
Gecko

Reputation: 1344

The problem is that you cannot post a single value once and retrieve it in two variables. If the form you stated (Default.cshtml) gets posted, the value posted will be like

user : [value input of #user]

There is only one value posted, linked to the name "user". And as we all know we can't have multiple parameters with identical names.

Upvotes: 1

Related Questions