【问题标题】:Binding two tables in model - ASP.NET MVC Razor在模型中绑定两个表 - ASP.NET MVC Razor
【发布时间】:2015-12-06 00:05:45
【问题描述】:

将模型与一类中的两个表绑定时遇到问题。将表单发布到控制器时,表单未将值绑定到模型。

型号:

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

User 和 Basic Profile 类是两个具有以下属性的表:

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
}

当我加载视图模型时,会显示在控制器(生成视图)或视图 c# 代码部分顶部初始化的任何值。 但是当我发布表单时,控制器显示的模型根本没有任何价值。 当我为单个表创建控制器/视图时,一切正常。这是控制器操作方法和视图的代码。

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

//这是视图

@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.MapRoute(
            name: "BasicRegister",
            url: "join",
            defaults: new { controller = "Home", action = "BasicRegister" }
            );

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

【问题讨论】:

  • User 和 BasicProfile 是如何关联的?似乎您缺少一对一的关系。
  • User 的 UserId 为 PK,BasicProfile 的 UserId 为 FK。一对一的关系。使用 Linq 到 sql。
  • 我想我只是对 BasicProfile 中没有公共用户用户 {get;set;} 的事实感到困惑。我不是说它应该是那样的。但我就是这样做的。 :) 我想如果你也想在类中显示关系。
  • 我在删除数据库中的关系后尝试过,但仍然不起作用。

标签: c# asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

您的模型是 null,因为您的模型有一个名为 basicProfile 的属性,而您的 POST 方法有一个同名的参数。

将方法中的参数名称改为(say)

[HttpPost]
public ActionResult DoBasicRegister(BasicProfileModel model) {

并且模型将被正确绑定。

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

【讨论】:

    【解决方案2】:

    路由配置不正确,我相信如果你能正确映射你的路由,它会起作用。试试下面的路线

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

    【讨论】:

    • 路线很好,Stephen Muecke 的回答解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多