Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have following Options.</p> <p><strong>1. Use Ajax Begin Form with AjaxOptions like below</strong></p> <pre><code>@using (Ajax.BeginForm("ActionName", "ControllerName", new { area = "AreaName" }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "alert('Success');" //This will execute once the Ajax call is finished. }, null)) { &lt;input type="submit" name="nameSubmit" value="Submit" /&gt; } </code></pre> <p><strong>2. Use JQuery to Manually Setup the XHR Request</strong></p> <pre><code>$.ajax({ url: "@Url.Action("ActionName", "ControllerName", new { area = "AreaName" });", type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify({param : Value}) }) .done(function () { alert('Success');}) //This will execute when you request is completed. .fail(function () { }) </code></pre> <p><strong>My Suggestions</strong></p> <p>There are following disadvantages while using the FormCollection</p> <p><strong>Point - 1</strong></p> <p>In case <code>FormCollection</code> is being used...It will be mandatory to <code>Type Cast</code> the <code>Primitive Type</code> Values un-necessarily because while getting the entry of specific Index of the <code>System.Collections.Specialized.NameValueCollection</code>, value being returned is of type <code>String</code>. This situation will not come in case of Strongly Typed <code>View-Models</code>.</p> <p><strong>Issue - 2</strong></p> <p>When you submit the form and goes to <code>Post</code> Action Method, and <code>View-Model</code> as Parameter exists in the Action method, you have the provision to send back the Posted Values to you <code>View</code>. Otherwise, write the code again to send back via <code>TempData/ViewData/ViewBag</code> </p> <p><img src="https://i.stack.imgur.com/MWl7n.png" alt="enter image description here"></p> <p><br><br></p> <p><strong>Point - 3</strong></p> <p>We have Data Annotations that can be implemented in <code>View Model</code> or <code>Custom Validations</code>. </p> <p><img src="https://i.stack.imgur.com/UmyKp.png" alt="enter image description here"></p> <p><em>ASP.Net MVC simplifies model validatons using Data Annotation. Data Annotations are attributes thyat are applied over properties. We can create custom validation Attribute by inheriting the built-in Validation Attribute class.</em></p> <p><br><br></p> <p><strong>Point - 4</strong></p> <p>Example you have the following <strong>HTML</strong></p> <pre><code>&lt;input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" /&gt; </code></pre> <p><strong><em>Question</strong> : How can we access the value of customAttr1 from the above eg from inside the controller</em></p> <p><strong>Answer</strong> : When a form get posted only the name and value of elements are posted back to the server. <em>You can also use Hidden Fields to post the Attributes to Post Action method</em>.</p> <p><strong>Alternatives</strong> : Use a bit of jQuery to get the custom attribute values, and post that along with the form values to action method</p> <p>Another option is to rather put what you got in your custom attributes in hidden controls</p> <p><br><br><br> That's the reason, I would always prefer to use <code>View-Models</code></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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