Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you pass your object back to the controller, you have to wrap your code in <code>If ModelState.IsValid</code> </p> <p>Below is a simplified version of how I edit a user. The first "EDIT" sends the User object TO the View. The second "EDIT" handles the post from the view.</p> <pre><code>Function Edit() As ActionResult ''# do stuff to populate your User Return View(User) End Function &lt;AcceptVerbs(HttpVerbs.Post)&gt; _ Function Edit(ByVal user as User) If ModelState.IsValid Then ''# do your valid stuff Else ''# The posted form was not valid, send the user back Return View(user) End If End Function </code></pre> <p>Here's the same thing in C#</p> <pre><code>public ActionResult Edit() { // do stuff to populate your User return View(User); } [AcceptVerbs(HttpVerbs.Post)] public object Edit(User user) { if (ModelState.IsValid) { // do your valid stuff } else { //'# The posted form was not valid, send the user back return View(user); } } </code></pre> <h1>EDIT:</h1> <p>On your view, if you want to add AJAX validation, just add the following.</p> <pre><code> &lt;% Html.EnableClientValidation() ''# This is where all the magic happens. It will build your clientside validation for you out of your MetaData. Using Html.BeginForm("Edit", "Users") %&gt; &lt;!-- all your markup crap --&gt; &lt;tr&gt; &lt;td&gt; &lt;%: Html.LabelFor(Function(model) model.UserName)%&gt;&lt;/td&gt; &lt;td&gt; &lt;%: Html.TextBoxFor(Function(model) model.UserName) %&gt; &lt;%: Html.ValidationMessage("UserName", "*")%&gt;&lt;br /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;!-- somewhere you'll want to add a Validation Summary of all your errors --&gt; &lt;%= Html.ValidationSummary("Oops!, please correct the errors...") %&gt; &lt;% End Using%&gt; &lt;!-- bottom of the page --&gt; &lt;script src="../../Assets/Scripts/MicrosoftAjax.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Assets/Scripts/MicrosoftMvcAjax.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Assets/Scripts/MicrosoftMvcValidation.js" type="text/javascript"&gt;&lt;/script&gt; </code></pre> <h1>EDIT:</h1> <p>Here is some info on rendering using <code>Ajax.BeginForm</code><br> <a href="http://singulartechnologies.com/asp-net-mvc-ajax-beginform-sample-code" rel="nofollow noreferrer">http://singulartechnologies.com/asp-net-mvc-ajax-beginform-sample-code</a><br> <a href="http://msdn.microsoft.com/en-us/library/dd381533.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/dd381533.aspx</a><br> <a href="http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx</a> </p>
    singulars
    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.
 

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