Note that there are some explanatory texts on larger screens.

plurals
  1. POError Posting edit data in ASP.NET MVC
    primarykey
    data
    text
    <p>Can someone explain why the model object is null. I checked the post values coming across the wire and all of them are populated.</p> <p>Using VS2010 Beta 2,WinXp SP2,however this works in VS2008 ??!!</p> <p>Yellow screen of death message</p> <blockquote> <p>Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p> <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p> <p>Source Error:</p> <pre><code>Line 17: &lt;p&gt; Line 18: &lt;label for="id"&gt;id:&lt;/label&gt; Line 19: &lt;%= Html.TextBox("id", Model.id) %&gt; &lt;--Error Line 20: &lt;%= Html.ValidationMessage("id", "*") %&gt; Line 21: &lt;/p&gt; </code></pre> </blockquote> <p>Controller Code</p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Contact contactToEdit) { if (contactToEdit.FirstName.Trim().Length == 0) ModelState.AddModelError("FirstName", "First name is required."); if (contactToEdit.Lastname.Trim().Length == 0) ModelState.AddModelError("LastName", "Last name is required."); if (contactToEdit.Phone.Length &gt; 0 &amp;&amp; !Regex.IsMatch(contactToEdit.Phone, @"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}")) ModelState.AddModelError("Phone", "Invalid phone number."); if (contactToEdit.Email.Length &gt; 0 &amp;&amp; !Regex.IsMatch(contactToEdit.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) ModelState.AddModelError("Email", "Invalid email address."); if (!ModelState.IsValid) return View(); try { // TODO: Add update logic here var con = (from c in _entities.Contacts where c.id == contactToEdit.id select c).FirstOrDefault(); _entities.ApplyCurrentValues(con.EntityKey.EntitySetName, contactToEdit); _entities.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } </code></pre> <p>Snippet of View code</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;JQGallery.Models.Contact&gt;" %&gt; ... &lt;%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p&gt; &lt;label for="id"&gt;id:&lt;/label&gt; &lt;%= Html.TextBox("id", Model.id) %&gt; &lt;%= Html.ValidationMessage("id", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="FirstName"&gt;FirstName:&lt;/label&gt; &lt;%= Html.TextBox("FirstName", Model.FirstName) %&gt; &lt;%= Html.ValidationMessage("FirstName", "*") %&gt; &lt;/p&gt; &lt;p&gt; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload