简体   繁体   English

绑定模型中的两个表-ASP.NET MVC Razor

[英]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. 当我加载视图模型时,显示在控制器(生成视图)或视图c#代码部分顶部初始化的任何值。 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" }
            );

Your model is null because your model has a property named basicProfile and your POST method has a parameter with the same name. 您的模型为null因为您的模型具有名为basicProfile的属性,而POST方法具有具有相同名称的参数。

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. 旁注:由于视图仅包含数据模型的一些属性,因此正确的方法是使用仅包含您在视图中显示/编辑的那些属性的视图模型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM