Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass nullable properties from ASP.NET view to Controller?
    primarykey
    data
    text
    <p>I've been reading stackoverflow for a few years now, but this is the first time I really need to post a question as I can't seem to find a suitable answer anywhere.</p> <p>The thing is, I want to pass an object (a LinqToSQL entity to be precise) to my View, allow the user to add some properties, and then post it back to save it.</p> <p>My object, called Probation, has a few properties (id, forpost, forcomment, user, starting, hours) and some related entity references. The forpost and forcomment entities are nullable as a user can be probated for making an offensive post or an offensive comment. Please mind that forpost refers to an Idea entity, I don't know what I was thinking calling it a post.</p> <p>So here's my controller logic:</p> <pre><code> [Authorize(Roles = "Admin, Moderator")] public ActionResult ProbateUser(int? post, int? comment) { if (post != null) { Idea idea = irep.GetIdea(post ?? 0); Probation probation = new Probation { user = (Guid)idea.user, Idea = idea }; return View(probation); } if (comment != null) { Comment rcomment = crep.GetComment(comment ?? 0); Probation probation = new Probation { user = rcomment.user, Comment = rcomment }; return View(probation); } return View("NotFound"); } [AcceptVerbs(HttpVerbs.Post)] [Authorize] public ActionResult ProbateUser(Probation probation, FormCollection values) { try { probation.starting = DateTime.Now; rep.ProbateUser(probation); rep.Save(); return RedirectToAction("Index"); } catch { return View("NotFound"); } } } </code></pre> <p>And here's the view, using hidden fields to store the preset values:</p> <pre><code> &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;%: Html.Hidden("Idea", Model.Idea)%&gt; &lt;%: Html.Hidden("Comment", Model.Comment)%&gt; &lt;%: Html.Hidden("user", Model.user) %&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.reason) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextAreaFor(model =&gt; model.reason) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.reason) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(model =&gt; model.hours) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextBoxFor(model =&gt; model.hours) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.hours) %&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; </code></pre> <p>Now my problem is, once the Probation object is posted back, the nullable fields forcomment and forpost are always null! </p> <p>So to summarize: what is the correct way of passing objects with nullable properties from Controller->View->Controller? Pretty please?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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