Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I differentiate an empty model from an initial model in an ASP.NET MVC 4 controller?
    text
    copied!<p>In my ASP.NET MVC application, I'm trying to implement the following concept:</p> <ul> <li>Browsing <code>http://.../PersonData/Show?id=A5668A31-9AAC-4A14-B471-7C9BA236961D</code> should display the data of the person identified by the given ID.</li> <li>Browsing <code>http://.../PersonData/Show</code> should display a view with a form where I can input an ID into a text box. When I submit the form, the browser should navigate, but not post, to the URL given above (i.e., the form's method is set to GET).</li> <li>When I submit the form without entering an ID (causing the URL <code>http://.../PersonData/Show?id=</code> to be browsed), ASP.NET MVC model validation should display a message that the ID is required. (With or without JavaScript being enabled.)</li> </ul> <p>To implement this, I build a model like this:</p> <pre><code>class Model { [Required] public Guid? Id { get; set; } } </code></pre> <p>And now I have the problem that when browsing <code>http://.../PersonData/Show</code> for the first time, ASP.NET model validation immediately gives an error that the ID is required because it does not distinguish between the case where the form was submitted without an ID (<code>?id=</code>) and the case where the action was invoked for the first time (id is not given at all).</p> <p>Ideas I've had:</p> <ul> <li>Use two different controller actions and URLs (like <code>http://.../PersonData/Search</code> and <code>http://.../PersonData/Show?id=</code>) to differentiate between first browsing and empty form submission.</li> <li>Use an additional form parameter (like <code>http://.../PersonData/Show</code> and <code>http://.../PersonData/Show?valueGiven=true&amp;id=</code>).</li> <li>Inspect the URL parameters from the action method using the <code>Request</code> object and clear <code>ModelState</code> errors for <code>"id"</code> if that key was not present in the URL.</li> </ul> <p>What is the best/suggested way of solving this? Is there a better fourth approach?</p> <p>(One thing that didn't work was to add two action methods with the same name for GET actions:</p> <pre><code>[HttpGet] public void Show() { /* return view with initial browsing logic */ } [HttpGet] public void Show(Model model) { /* return view with subsequent browsing logic */ } </code></pre> <p>This seems not to be allowed by ASP.NET MVC.)</p> <p><strong>Update 2013-06-25:</strong> Sorry, there were two mistake in my sample, the ID should have been a <code>Guid</code>, not a string; and of course, I'm binding the Model in my action method because I need the <code>Required</code> attribute to be evaluated.</p> <p>And this is my routing configuration:</p> <pre><code>public static void RegisterRoutes (RouteCollection routes) { routes.IgnoreRoute ("{resource}.axd/{*pathInfo}"); routes.MapRoute ( name: "Default", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" } ); } </code></pre>
 

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