Save user form input to model and display in view (“Object reference not set to an instance of an object”)
Save user form input to model and display in view (“Object reference not set to an instance of an object”)
Edit;
Sharing the code as it looks after some changes aswell as with the right variable names etc.
So I have an input form that looks like this:
@model InluppEtt.Models.UserInfo
@using (Html.BeginForm("Hello", "Second", FormMethod.Post))
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.LabelFor(m => m.Age)
@Html.TextBoxFor(m => m.Age)
@Html.LabelFor(m => m.Location)
@Html.TextBoxFor(m => m.Location)
<input type="submit" value="Send" />
@Html.DisplayName(Model.DisplayInfo)
It supposed to get posted to this action:
[HttpPost]
public ActionResult Hello(UserInfo userInfo)
var ui = new UserInfo()
Name = userInfo.Name,
Age = userInfo.Age,
Location = userInfo.Location
;
ViewBag.Message = "Success";
return View("Hello", ui);
Now I am trying to save the input into this model here but I am unable to make it work it seems.
public class UserInfo
public String Name get; set;
public String Age get; set;
public String Location get; set;
public String DisplayInfo
get
return "Name: " + Name + " Age: "
+ Age + " Location: " + Location;
I am trying to show the data underneath the form in the view but as I visit that page the first thing I get is an "Object reference not set to an instance of an object." error at the
@Html.DisplayName(Model.DisplayInfo)
Thanks
Html.BeginForm()
Hello
Request
@Model.DisplayText
@Html.DisplayText
2 Answers
2
Edit
view:
@model Test.Models.UserInfo
@
ViewBag.Title = "Hello";
Layout = "~/Views/Shared/_Layout.cshtml";
@using (Html.BeginForm("Hello", "Test"))
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.LabelFor(m => m.Age)
@Html.TextBoxFor(m => m.Age)
@Html.LabelFor(m => m.Location)
@Html.TextBoxFor(m => m.Location)
<input type="submit" value="Send" />
@ViewBag.Message
<br />
@Html.DisplayName(Model.DisplayInfo)
controller:
using System;
using System.Web.Mvc;
using Test.Models;
namespace Test.Controllers
public class TestController : Controller
public ActionResult Hello(UserInfo userInfo)
if (!String.IsNullOrEmpty(userInfo.Name))
ViewBag.Message = "Success";
return View(userInfo);
model:
using System;
namespace Test.Models
public class UserInfo
public String Name get; set;
public String Age get; set;
public String Location get; set;
public String DisplayInfo => "Name: " + Name + " Age: " + Age + " Location: " + Location;
Thank you for the answer, it seems promising but the @Html.DisplayName causes a null reference exception
– Sandra
Sep 17 '18 at 2:20
There shouldn't be any null reference.
return View("location_to_your_cshtml_view_file", displayInfo);
in this line you are passing the model so your view will surely recognize it, if you do not pass displayInfo
here only then you should get null reference. you can also use View(displayInfo);
if your view page is automatically recognized by the action method.– Wahid Masud
Sep 17 '18 at 7:31
return View("location_to_your_cshtml_view_file", displayInfo);
displayInfo
View(displayInfo);
who down voted this? care to explain?
– Wahid Masud
Sep 18 '18 at 8:03
see the edited version. you never told the input form and view were same. next time you post a question you be crystal clear about what you have and what you want.
– Wahid Masud
Sep 18 '18 at 9:09
Yes, because you(DefaultModelBinder) are accessing object without initializing it into your view GET Request. I hope there is no object instantiation in your [httpget]
of ActionMethod. You need to instantiate first then pass to your ActionMethod get request and this will show empty string (what value types are initialized to default).
[httpget]
[HttpGet]
public ActionResult Hello(UserInfo userInfo)
var ui = new UserInfo();
return View("Hello", ui);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
This is a bit confusing. your
Html.BeginForm()
has no action name or method and still the form is posted. you are using a model but not using a model binding. besides, inside yourHello
action you are creating an instance of your model and saving the values directly fromRequest
. so you are already saving them. are you trying to send that instance to another view? and what is@Model.DisplayText
? you mean@Html.DisplayText
?– Wahid Masud
Sep 16 '18 at 19:55