user1211185
user1211185

Reputation: 731

MVC3 - razor: error while passing query string

I'm a new bie with MVC3 razor. Can anyone please help me why I am getting this error on run.

Error: Object reference not set to an instance of an object. it breaks on ActionLink.

HTML Code:

@model Solution.User

@using (Html.BeginForm())
{
    @Html.TextBoxFor(model => model.Name, new {@id = "name-ref", @class = "text size-40"})
    @Html.ActionLink("Go Ahead", "Index", "Home", new {name = Model.name, @class = "button" })
}

Controller

[HttpPost]
public ActionResult Index(string name)
{
    return View();
}

Many Thanks

Upvotes: 2

Views: 516

Answers (1)

moribvndvs
moribvndvs

Reputation: 42497

You haven't supplied a model to the view.

Define a class to act as the view model

public class User
{
    public string Name { get; set; }
}

And in your controller's action:

[HttpPost]
public ActionResult Index(User model)
{
    return View(model);
}

MVC's model binder will automatically create an instance for the parameter model and bind the name value to User.Name for you.

Edit Your view mentions a model called User. I changed my answer to reflect that.

Upvotes: 3

Related Questions