0

Is there any way to highlight/ style DisplayFor or/and DisplayNameFor fields depending on validation defined in the model?

It is in a strongly typed partial view and I'm using umbraco 7

@inherits  UmbracoViewPage<MyProject.Models.ImportViewModel>
@{
    Layout = "~/Views/Master.cshtml";
}    
<table class="list">
    @foreach (var item in Model.Items)
{
    var categories = (IEnumerable<MyProject.Models.Category>)ViewData["categories"];

    <tr class="list-item">
        <td>
            <table>
                <tbody>
                    <tr>
                        <td colspan="3">
                            <h2>
                                (@Html.DisplayFor(modelItem => item.Ref))
                                @Html.DisplayFor(modelItem => item.Title)
                            </h2>
                        </td>
                        <td>
                            <label class="auto">@Html.DisplayNameFor(model => item.Status)</label>
                            @Html.DisplayFor(modelItem => item.Status)<br /><br />

                            @Html.ActionLink("Approve", "Approve", new { id = item.Id })
                        </td>
                    </tr>



                    <tr>
                        <td>
                            <label>Category:</label> @Html.DisplayFor(modelItem => categories.First(m => m.Id == item.CategoryId).Name)
                        </td>
                        <td>
                            <label>@Html.DisplayNameFor(model => item.Description)</label>
                            @Html.DisplayFor(modelItem => item.DescriptionShort)
                        </td>
                    </tr>
                </tbody>
            </table>

        </td>
    </tr>
}

</table>

I have this in my controller:

List<MyProject.Models.Import> items = (List<MyProject.Models.Import>)Session["sesimp"];
return PartialView("ItemList", new ImportViewModel { Items = items });

I want to highlight fields which are not correct based on the field validation defined in the model.

So for example if category is not selected and it is defined as required in model) then this field on the list has different css class assigned and/or possibly display error message. Same if Ref is empty, Title or Description contains invalid characters etc.

Also I need to disable Approve link if the item didn't pass validation.

How to do it?

tereško
  • 58,060
  • 25
  • 98
  • 150
nickornotto
  • 1,946
  • 4
  • 36
  • 68
  • Not clear why you would be allowing invalid models when creating or editing them but you could add additional properties to your view model and then validate each model in a loop e.g. `bool IsValid = TryValidateModel(yourModel);` and use the property to determine if the `Approve` link should be disabled –  Oct 02 '14 at 10:33
  • loki2302 solved a similar problem for adding a class when a specific field is invalid (which you could use for highlighting) see - http://stackoverflow.com/a/16448404/201648. Likewise, Jim Schubert wrote a jQuery handler which checks for invalid fields and allows you to apply some logic (such as disabling a button http://stackoverflow.com/a/7067346/201648. For enabling/disabling a button see http://stackoverflow.com/a/1594992/201648 (OR disabling a link http://stackoverflow.com/a/970413/201648) – Aaron Newton Oct 03 '14 at 07:03
  • Thank you @AaronNewton. I will try solutions you suggest. – nickornotto Oct 03 '14 at 09:40
  • @StephenMuecke I need to show records first so user see they need editing or not before processing them – nickornotto Oct 03 '14 at 09:40
  • Yes, but why would you allow invalid models when you first create them? Note @AaronNewton suggested links are not relevant because you don't have a form or inputs –  Oct 03 '14 at 09:49
  • Hmmm... Stephen is right. I was basing my answer on using unobtrusive validation on a form, e.g. http://www.codeproject.com/Articles/577937/A-Beginners-Tutorial-on-Validating-Model-Data-and. And YES, you should be checking _if (ModelState.IsValid)_ on postback of the original form. A possible scenario where I could see this happening is when data is created with one application not enforcing the validation logic, and then loaded into another which does. I suspect first approach might work, but I haven't tried it under these conditions stackoverflow.com/a/16448404/201648 – Aaron Newton Oct 03 '14 at 10:24
  • @StephenMuecke I am importing the data from Excel spreadsheet to a session and performing malware validation on import. What I need to do then is to allow users to check the records are correct for the site and import them to the database. – nickornotto Jan 14 '15 at 14:52

0 Answers0