Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I understand what you are trying to do, here is an example of what I would do:</p> <p>Create a view model which uses Data Annotations to enforce client side validation (for mandatory fields and string length etc.):</p> <pre><code>public class CreateViewModel { public SelectList Reasons { get; set; } //to store list of reasons from db public string Reason { get; set; } //to store selected reason [Required] public DateTime StartDate {get; set;} [Required] [StringLength(250, ErrorMessage = "Details must be less than 250 characters")] public string Details { get; set; } } </code></pre> <p>Have a view that is strongly typed to the DetailsViewModel and uses Html.EnableClientValidation to turn on the client side validation and Html.ValidationMessageFor to show validation messages:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MvcApplication1.Models.CreateViewModel&gt;" %&gt; &lt;% Html.EnableClientValidation(); %&gt; &lt;% Html.ValidationSummary(); %&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;div&gt; &lt;label for="Reason"&gt;Reason:&lt;/label&gt; &lt;/div&gt; &lt;div&gt; &lt;%= Html.DropDownListFor(m =&gt; m.Reason,Model.Reasons, "Select Reason")%&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="StartDate"&gt;Start Date:&lt;/label&gt; &lt;/div&gt; &lt;div&gt; &lt;%= Html.TextBoxFor(m =&gt; m.StartDate)%&gt; &lt;%= Html.ValidationMessageFor(m =&gt; m.StartDate %&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="Details"&gt;Details:&lt;/label&gt; &lt;/div&gt; &lt;div&gt; &lt;%=Html.TextAreaFor(m =&gt; m.Details)%&gt; &lt;%= Html.ValidationMessageFor(m =&gt; m.Details %&gt; &lt;/div&gt; &lt;input type="submit" value="Submit" /&gt; &lt;input type="reset" value="Cancel" /&gt; </code></pre> <p>&lt;% } %></p> <p>And in your controller method Get method to display the view:</p> <pre><code>[HttpGet] public ActionResult Create() { var viewModel = new CreateViewModel(); //get reasons from DB var reasons = from reason in db.Reasons select reason; //set the reason Id as the unique identifier of the reason and the reason text to be what will be displayed in the dropdown var reasonItems = reasons.Select(r =&gt; new { ShortCode = reasons.Id, Definition = reasons.reasonText }); //create a select list that will select the id as the value and show the definition in the label viewModel.Reasons = new SelectList(reasonItems, "ShortCode", "Definition"); return View("", viewModel); //return the populated dropdown to the view } </code></pre> <p>and in your controller Post method check use ModelState.IsValid to check if the view data is and if not add model state errors to appear on the view page:</p> <pre><code>[HttpPost] public ActionResult Create(CreateViewModel viewModel) { if (ModelState.IsValid) { // go and save your view model data } ModelState.AddModelError("Error", "Values are not valid"); return RedirectToAction("Create"); } </code></pre> <p>In this case I simply add a model state error and then redirect to the controller Get method which will show the view again with the model error (that is what the Html.ValidationSummary code in the view does - show the model state errors automatically).</p> <p>I hope this helps.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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