gpt4 book ai didi

asp.net-mvc - 从部分 View 问题返回模型

转载 作者:行者123 更新时间:2023-12-02 13:34:29 25 4
gpt4 key购买 nike

我创建了一个部分 View (即使是编辑器模板),我将一个子模型传递给该 View ,但是,当我单击“提交”时,我总是从部分 View 中得到“null”。我可以获得除子模型之外的主模型的属性值。

主要型号

public class PetModel
{
public string name {get; set;}
public long SpeciesID {get; set;}
public long BreedID {get; set;}
public Calendar DOB {get; set;}
}

子模型

public class Calendar
{
public string Year{get; set;}
public string Month{get; set;}
public string Day{get; set;}
}

主视图

    @model Application.Models.PetModel
@using (Html.BeginForm("CatchPetContent", "Quote",Model))
{
@Html.TextBoxFor(x => x.Name)
@Html.DropDownListFor(x=>x.SpeciesID,new List<SelectListItem>(),"select")
@Html.DropDownListFor(x=>x.BreedID,new List<SelectListItem>(),"select")
@Html.EditorFor(Model => x.DOB)
<input type="submit" value="submit" />
}

编辑器模板

@model Application.Models.Calendar
@Html.DropDownListFor(Model => Model.Day, new List<SelectListItem>())
@Html.DropDownListFor(Model => Model.Month,new List<SelectListItem>())
@Html.DropDownListFor(Model => Model.Year, new List<SelectListItem>())

“CatchPetContent”操作

[HttpPost]
public ActionResult CatchPetContent(PetModel Model)
{


PetModel pet = new PetModel();
pet.Name = Model.Name;
pet.SpeciesID = Model.SpeciesID;
pet.BreedID = Model.BreedID;
pet.DOB = Model.DOB;// always null

RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("Controller", "Home");
redirectTargetDictionary.Add("Action", "Index");

return new RedirectToRouteResult(new RouteValueDictionary(redirectTargetDictionary));

}

当我调试它时,“Model.DOB”始终为空

最佳答案

您应该将子属性添加为操作的额外参数:

[HttpPost]
public ActionResult CatchPetContent(PetModel Model, Calendar Bob)
{
** snip **
}

默认的 ModelBinder 不嵌套对象。但是,如果您将其作为第二个参数包含在内,它确实会找到这些值。

如果你想嵌套它们,你必须创建自己的模型绑定(bind)器。

以下问题也有类似的问题:List count empty when passing from view to model in ASP.Net MVC

关于asp.net-mvc - 从部分 View 问题返回模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com