ASP.Net MVC: gewusst Wie: anpassen der Validierung angezeigt

unten ist der code, wo ich das generieren einer html-Tabelle, die in for-Schleife und binden textbox & dropdown. auch die Verwendung von jquery unobtrusive-Bibliothek zu überprüfen textbox & dropdown.

alle arbeiten, aber ich wollen, anpassen der Validierung etwas anders.in meinem Fall die Validierung von Nachrichten zeigen, aber ich möchte Validierung Nachricht wird nicht angezeigt, sondern wenn der Benutzer geht mit der Maus-cursor auf die rot umrandeten Textfeld oder roten Rand dropdown-dann validierungsmeldung wird angezeigt, wie ein tool-Tipp.

wie kann ich anfügen-Validierung Nachricht an title-Attribut des Textfeld und dropdown ?

hier ist mein vollständiger code. bitte sehen und kommen mit Vorschlag oder berichtigt werden Beispiel wie, was hinzufügen oder ändern in mein vorhandenen code zu erreichen, was ich will.

Modell-und view-Modell

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace RemoveValidateMessage.Models
{
    public class MainViewModel
    {
        public List<Student> Students { get; set; }
        public int SelectedState = 0;
        public int SelectedCity = 0;
    }

    public class Student
    {
        [Required]
        public int ID { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public int StateID { get; set; }

        [Required]
        public int CityID { get; set; }
        public List<States> States { get; set; }
        public List<Cities> Cities { get; set; }
    }

    public class States
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class Cities
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

Controller

using RemoveValidateMessage.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RemoveValidateMessage.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            MainViewModel oVm = new MainViewModel()
            {
                Students = new List<Student>() {
                    new Student
                    {
                        ID=1,
                        Name="JoyDev",
                        StateID=1,
                        CityID=1,
                        States=new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        },
                        Cities=new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Alipur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Asansol"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Andul"
                            }

                        }
                    },

//***********
                    new Student
                    {
                        ID=1,
                        Name="Mukti",
                        StateID=2,
                        CityID=1,
                        States=new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        },
                        Cities=new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Janpur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Madhubani"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Kanti"
                            }

                        }
                    },
//***********
                    new Student
                    {
                        ID=1,
                        Name="Somnath",
                        StateID=3,
                        CityID=2,
                        States=new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        },
                        Cities=new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Chandapur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Dhankauda"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Konarak"
                            }

                        }
                    }


                }

            };


            return View(oVm);
        }

        [HttpPost]
        public ActionResult Index(MainViewModel model)
        {
            //if (ModelState.IsValid)
            //{
            //   return View(model);
            //}
            for (int i = 0; i < model.Students.Count;i++ )
            {
                model.Students[i].States = new List<States>()
                        {
                            new States
                            {
                                ID=1,
                                Name="WestBengal",
                            },
                            new States
                            {
                                ID=2,
                                Name="Bihar",
                            },
                            new States
                            {
                                ID=3,
                                Name="Orrisa",
                            }

                        };
                model.Students[i].Cities = new List<Cities>()
                        {
                            new Cities
                            {
                                ID=1,
                                Name="Chandapur"
                            },
                            new Cities
                            {
                                ID=2,
                                Name="Dhankauda"
                            },
                            new Cities
                            {
                                ID=3,
                                Name="Konarak"
                            }

                        };
            }
            return View(model);
        }
        public ActionResult Test()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            ViewBag.Time = DateTime.Now.ToString();
            return View();
        }


        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

Anzeigen-Code

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@model RemoveValidateMessage.Models.MainViewModel
@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm("Index", "Home",FormMethod.Post))
{ 
<div>
    <table>
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>State</td>
            <td>City</td>
        </tr>
        @for (int i = 0; i < Model.Students.Count; i++)
        {
            <tr>
                <td>
                    @*<input type="text" value="@Model.Students[i].ID" />*@
                @Html.EditorFor(m=>m.Students[i].ID)
                @Html.ValidationMessageFor(m => m.Students[i].ID)
                </td>

                <td>
                @*<input type="text" value="@Model.Students[i].Name" />*@
                    @Html.EditorFor(m => m.Students[i].Name)
                    @Html.ValidationMessageFor(m => m.Students[i].Name)
                </td>
                <td>
                    @Html.DropDownListFor(m => m.Students[i].StateID, new SelectList(Model.Students[i].States, "ID", "Name", Model.Students[i].StateID), "-- Select States--", new { @class = "edit-mode" })
                    @Html.ValidationMessageFor(m => m.Students[i].StateID)
                </td>
                <td>
                   @Html.DropDownListFor(m => m.Students[i].CityID, new SelectList(Model.Students[i].Cities, "ID", "Name", Model.Students[i].CityID), "--Select States--", new { @class = "edit-model" })
                    @Html.ValidationMessageFor(m => m.Students[i].CityID)
                </td>
            </tr>
        }
    </table>
</div>
 <p><input type="submit" value="Submit" /></p>
}

Screenshot

ASP.Net MVC: gewusst Wie: anpassen der Validierung angezeigt

InformationsquelleAutor Mou | 2016-01-15
Schreibe einen Kommentar