Неверная проверка JQuery, похоже, работает на основе модели, переданной на страницу, но что, если я хочу иметь более одной модели в игре?Несколько форм на странице с ненавязчивой проверкой jquery в ASP.NET MVC
Скажем "MyPage" имеет две формы, каждая посылка обратно к другому действий, с другой моделью
Модели
public class MyPageModel
{
public List<Student> Students { get; set; }
public List<Prof> Profs { get; set; }
[Required]
public string Student { get; set; } // wrong wrong wrong
[Required]
public string Prof { get; set; } // so wrong. this can't be the way
}
public class AddStudentModel
{
[Required]
public string Student { get; set; }
}
public class AddProfModel
{
[Required]
public string Prof { get; set; }
}
Посмотреть
// MyPage!
// list students here
@using (Html.BeginForm("AddStudent", "Controller", new { }, FormMethod.Post))
{
@Html.TextBox("Student", null, new { })
@Html.ValidationMessageFor("Student")
<input type="submit" value="add student" />
}
// list professors here
@using (Html.BeginForm("AddProf", "Controller", new { }, FormMethod.Post))
{
@Html.TextBox("Prof", null, new { })
@Html.ValidationMessageFor("Prof")
<input type="submit" value="add prof" />
}
Контроллер
public ActionResult MyPage()
{
MyPageModel model = new MyPageModel();
// bind data here
return View(model);
}
public ActionResult AddStudent(AddStudentModel model)
{
// add student
return RedirectToAction("MyPage");
}
public ActionResult AddProf(AddProfModel model)
{
// add professor
return RedirectToAction("MyPage");
}
До сих пор я добавлял пустой Student
/Prof
свойствам MyPageModel
, но это чувствует себя очень взломанным. Есть ли способ указать модель в Html.BeginForm
, которую будет использовать проверка jquery?