Joy
Joy

Reputation: 7028

Save data from diffrent partial views on Single view on single button submit in mvc 3

i have three classes

  public partial class User
{          
    public string No_ { get; set; }      
    public string LastName { get; set; }   
    public virtual ICollection<Login> Logins { get; set; }  
    public virtual ICollection<Education> Educations { get; set; }
} 
public partial class Education
{
    public string No_ { get; set; }
    public string UserId { get; set; }
    public string Degree { get; set; }
    public string Institution { get; set; }
    public string Percentage { get; set; }
}
public partial class Login
{        
    public string No_ { get; set; }     
    public string UserId { get; set; }       
    public string Username { get; set; }
    public string Password { get; set; }
    public virtual User User { get; set; }
}

i have made three partial views for three diffrent models and made it render into a page as below

@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/BlankLayout.cshtml";
 }
 <h2>
Register</h2>@using (@Html.BeginForm())
{
      @Html.Partial("LoginPartialView")

      @Html.Partial("UserPartialView")

      @Html.Partial("ProfessionPartialView")

      <section>
          <div>
             <button class="reset">Reset</button>
             <button class="submit" name="submit" value="Submit">Submit</button>
          </div>
   </section>
}

what i want is when i click the submit button all the data from the partial views should get to [httppost] where i can save the data to User,Education,Login Tables . how to get the data into the controller which has http post controller like :

[HttpPost]
    public ActionResult Register(?,?,?)
    {
        context.Logins.Add(LoginObject);
        context.Educations.Add(EducationObject);
        context.Professions.Add(ProfessionObject);
        return View();
    }

i just want to know how to get the above partial views data into the httppost controller so that i can save the data as mentioned above

i am pretty much novice in Mvc 3 Pardon me if i am making no sense while i am asking . please guide me along with

Upvotes: 2

Views: 4055

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

Instead of partials I would recommend you using editor templates. Here's an example how you could write a form that will save the User object:

@model User
@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/BlankLayout.cshtml";
}
<h2>
Register</h2>
@using (@Html.BeginForm())
{
    @Html.EditorFor(x => x.No_)
    @Html.EditorFor(x => x.LastName)

    @Html.EditorFor(x => x.Logins)
    @Html.EditorFor(x => x.Educations)

    <section>
        <div>
            <button class="reset">Reset</button>
            <button class="submit" name="submit" value="Submit">Submit</button>
        </div>
   </section>
}

and then the 2 corresponding editor templates:

~/Views/Shared/EditorTemplates/Login.cshtml:

@model Login
... some input fields for the login

~/Views/Shared/EditorTemplates/Education.cshtml:

@model Education
... some input fields for the education

and the controller action:

[HttpPost]
public ActionResult Register(User model)
{
    // the model object will be correctly populated from the default model binder
    // here we can save it
    return View(model);
}

Upvotes: 3

Related Questions