nicedev92
nicedev92

Reputation: 1705

Binding two tables in model - ASP.NET MVC Razor

I am facing problem when binding model with two tables in one class. Form is not binding values to model when posting it to controller.

Model:

public class BasicProfileModel {
    public BasicProfileModel() {
        this.user = new User();
        this.basicProfile = new BasicProfile();
    }
    public User user { get; set; }
    public BasicProfile basicProfile { get; set; }        
}

User and Basic Profile classes are two tables with following properties:

public class User {
    public string UserId { get; set; }//not null and PK
    public string EmailId { get; set; }//no null
    public string Salt { get; set; }//not null
    public string Password { get; set; }//not null
    public string Role { get; set; }//not null
    public bool HasCompleteProfile { get; set; }//not null
}

public class BasicProfile {
    public string UserId { get; set; }//not null FK
    public string FirstName { get; set; }//not null
    public string LastName { get; set; }
    public string Gender { get; set; }//not null
    public int YearOfBirth { get; set; }//not null
    public bool IsApprovedByUser { get; set; }//not null
    public bool IsApprovedByAdmin { get; set; }//not null
    public bool IsActiveByUser { get; set; }//not null
    public bool isActiveByAdmin { get; set; }//not null
}

When I load view model shows any value initialised in controller (that generates view) or in top of the view c# code section. But when I post the form the controller shows model without any value at all. When I create controllers/views for single table then every thing works fine. Here is code for controller action methods and view.

public ActionResult BasicRegister() {
        BasicProfileModel model = new BasicProfileModel();

        //any value initialised in model here displays in view elements e.g. text boxes.
        return View("BasicRegister", model);
    }

    [HttpPost]
    public ActionResult DoBasicRegister(BasicProfileModel basicProfile) {
        return View("BasicRegister", basicProfile);
    }

//This is view

@model BasicProfileModel


@using (Html.BeginRouteForm("DoBasicRegister", FormMethod.Post)) {
<div>
    @Html.TextBoxFor(m => m.user.EmailId)
</div>
<div>
    @Html.TextBoxFor(m => m.user.Password)
</div>
<div>
    @Html.TextBoxFor(m => m.basicProfile.FirstName)
</div>

<div>
    @Html.TextBoxFor(m => m.basicProfile.LastName)
</div>
<div>
    <input type="submit" value="send" />
</div>

}

//Routes

routes.MapRoute(
            name: "BasicRegister",
            url: "join",
            defaults: new { controller = "Home", action = "BasicRegister" }
            );

        routes.MapRoute(
            name: "DoBasicRegister",
            url: "dojoin",
            defaults: new { controller = "Home", action = "DoBasicRegister" }
            );

Upvotes: 2

Views: 1853

Answers (2)

Julius Depulla
Julius Depulla

Reputation: 1633

The route is not configured correctly, I have believe if you can get your routes mapped correctly, it will work. Try the routes below

routes.MapRoute(
            name: "BasicRegister",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "BasicRegister", id = UrlParameter.Optional }
            );

        routes.MapRoute(
            name: "DoBasicRegister",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "DoBasicRegister" , id = UrlParameter.Optional}
            );

Upvotes: 0

user3559349
user3559349

Reputation:

Your model is null because your model has a property named basicProfile and your POST method has a parameter with the same name.

Change the name of the parameter in the method to (say)

[HttpPost]
public ActionResult DoBasicRegister(BasicProfileModel model) {

and the model will be correctly bound.

Side note: Since your view only includes a few properties of your data models, the correct approach is to use a view model that includes only those properties you display/edit in the view.

Upvotes: 1

Related Questions