2

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

1 Answer 1

3

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.

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.