I have the following which I want to POST to create when I hit submit. Only, when I hit submit I get an object reference error referring to my value for the input with id = debugTxt. I suspect that because I’ve lost the object state, I get this error.
So my question is how can I set the text with an initial view that comes from the Model and allow the user to update on POST?
@using (Html.BeginForm("Create", "PhysDoc"))
{
<table>
<tr>
<td class="title">Debug Mode</td>
<td>
This input does the initial GET correctly. On POST I get object ref error related to the value inside @Model.
<input type="text" id="debugTxt" name="debugModeTxt" value="@Model.DebugMode" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
Create Method (Note the exception is not thrown here).
[HttpPost]
public ActionResult Create(string debugModeTxt)
{
PhysdocSettings settings = new PhysdocSettings();
settings.DebugMode = bool.Parse(debugModeTxt);
PhysDocSettingsBL settingBL = new PhysDocSettingsBL();
settingBL.UpdateSettings(settings);
return View("Index");
}
The exception occurs here:
value="@Model.DebugMode" and if I change value="True" my code works fine. But this doesn’t give me an initial value equal to Model.DebugMode.
Try this… Use an HTML helper and specify a default value for the field like so:
If this doesn’t work can you elaborate on the result?
You aren’t passing a model to Index when you call
return View("Index");. You are merely calling the view and as such you will indeed get a Null Reference Exception.