2016-07-06 33 views
0

JQuery unaufdringliche Validierung scheint basierend auf dem Modell auf der Seite übergeben - aber was, wenn ich mehr als ein Modell im Spiel haben möchte?Mehrere Formulare auf Seite mit jquery unaufdringliche Validierung in ASP.NET MVC

Sagen wir "MyPage" zwei Formen, die jeweils Entsendung zurück in eine andere Aktion, mit einem anderen Modell

Modelle

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; } 
} 

Ansicht

// 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" /> 
} 

-Controller

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"); 
} 

Bisher habe ich leere Student/Prof Eigenschaften zu MyPageModel hinzugefügt, aber das fühlt sich sehr hacky. Gibt es eine Möglichkeit, ein Modell in der Html.BeginForm anzugeben, die Jquery-Validierung verwendet?

Antwort

2

Sie könnten Kind Aktionen für diese, und entfernen Sie Ihre persönlichen Eigenschaften von Ihrem MyPageModel:

public ActionResult MyPage() 
{ 
    MyPageModel model = new MyPageModel(); 
    // bind data here 
    return View(model); 
} 

[HttpGet] 
public ActionResult AddStudent() 
{ 
    return PartialView(new AddStudentModel()); 
} 

[HttpPost] 
public ActionResult AddStudent(AddStudentModel model) 
{ 
    //add student 
    return RedirectToAction("MyPage"); 
} 

Aktuelle MyPage:

//MyPage! 
@Html.Action("AddStudent", "SomeController") 

@Html.Action("AddProf", "SomeController") 

Neue Ansicht durch neue Kind Aktion zurückgegeben.

//AddStudent.cshtml 

@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(m => m.Student) 
    @Html.ValidationMessageFor(m => m.Student) 
    <input type="submit" value="add student" /> 
} 

Um dies eine glatte Lösung zu machen, es könnte einen Blick auf Ajax.BeginForm nimmt wert sein, wie es wird können Sie auf der Seite ein Formular zu einem Zeitpunkt, aktualisieren (und zurück eine Teilansicht als reponse Bei einer Formularübergabe muss also nicht die gesamte Seite aktualisiert werden).