0

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

7
  • You do not provide nearly enough information to make an educated guess, much less a proper diagnosis. Commented Nov 26, 2013 at 5:29
  • (string user, TestClass tc) -> user has been resolved. But tc instance is not populated with the user information in the given action method. Commented Nov 26, 2013 at 5:33
  • By the way, which event are you navigating from? Commented Nov 26, 2013 at 5:35
  • i guess you are getting an empty "" to the user(1st parameter) right? when your previous page is not strongly typed you might be just creating class in your parameter(2nd parameter) and invoking it so getting null. Commented Nov 26, 2013 at 5:41
  • @Saurabh I got values in first parameter - user. Commented Nov 26, 2013 at 5:45

1 Answer 1

1

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.

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.