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

时间:2015-12-06 00:05:46

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

在一个类中使用两个表绑定模型时遇到问题。将表单发布到控制器时,表单不是模型的绑定值。

型号:

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

2 个答案:

答案 0 :(得分:1)

您的模型为* Writing 60 items * Suite 0-sub0-sub0-sub0-sub0-sub0 complete ["50.537813","51.7504874","51.8143035","53.4737368","52.4913574","51.1978692","53.5920406","53.4900032","53.49275","51.5024151","52.1984359","52.13419 19","53.8636212","51.5384084","51.5371952","48.1181396","52.151562","52.4346399","48.0577744","52.6403921","52.2545572","52.2504471","50.0440849","48. 3681909","49.6808955","52.6338293","53.6031792","51.3843645","49.824228","52.2096506","47.5663295","54.1074032","50.1156358","53.3241788","54.6345055" ,"52.322425","50.8221216","50.1329512","48.2611337","49.8259861","52.2488409","52.4392467","53.0738941","50.1053255","50.1015457","50.1041568","50.680 7764","53.5991453","51.6823706","53.4000913","51.2493895","51.0344969","53.0784862","54.7855778","51.6745303","52.8624927","47.951209","48.304389","49 .9745581","52.1767346"] * Saved 60 items ,因为您的模型具有名为null的属性,并且您的POST方法具有相同名称的参数。

将方法中参数的名称更改为(比方说)

basicProfile

并且模型将被正确绑定。

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

答案 1 :(得分:0)

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

$Dict = Get-Content "C:\Users\----\Desktop\Powershell Program\US.txt"

if($Right -ne "") {
    $Comb = $Letter + $Right
    $total = [int]0    
    $F = ""

    do {
        $F = $Dict | Select-Object -Index $total
        if($F.Length -eq $Num) {
            if($F.Chars("0") + $F.Chars("1") -eq $Comb) {
                Add-Content "C:\Users\----\Desktop\Powershell Program\Results.txt" "$F"
            }
        }
        $total++
        Write-Host $total
    } until([int]$total -gt [int]118619)

    $total = [int]0
    $F = ""
}