gpt4 book ai didi

asp.net-mvc - Asp.Net MVC - 模型绑定(bind)到模型或 ViewModel

转载 作者:行者123 更新时间:2023-12-01 13:50:13 24 4
gpt4 key购买 nike

我有一个 Controller ,它返回一个传入 View 模型的 View ,该 View 模型具有显示 View 所需的属性(下拉选择项列表等)。

但是当我将它发布到服务器时,我有一个不同的模型类,它具有那些下拉列表的选定值。在我的 HttpPost Controller 操作中,我在进行任何处理之前检查了 (ModelState.IsValid),但是当它为 false 时,我“返回 View(model)”。

但由于 View 绑定(bind)到 ViewModel,而我的 Post 操作正在接受实际模型,我收到错误消息“传递到字典中的模型项的类型为“模型”,但该字典需要“ViewModel”类型的模型项,当我提交表单时,验证错误会显示在 View 上。

我该如何解决这个问题?使用强类型 View 、传入 View 模型但提交到不同模型时的最佳实践是什么?

代码:

 public ActionResult Buy()
{
BuyVM buyVM = GetBuyVM();
return View(buyVM);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Buy(BuyModel model)
{
if (ModelState.IsValid)
{
// Do Procesing
return View("Success");
}
return View(model);
}

public class BuyVM
{
public SelectList PurchaseDateList { get; set; }

public SelectList BedroomsList { get; set; }

public SelectList StoriesList { get; set; }

[Required]
public string SquareFootage { get; set; }

[Required]
public string PreferredCityLocations { get; set; }

public string AdditionalInfo { get; set; }
}

public class BuyModel
{
public string PurchaseDateList { get; set; }
public string BedroomsList { get; set; }
public string StoriesList { get; set; }
public string SquareFootage { get; set; }
public string PreferredCityLocations { get; set; }
public string AdditionalInfo { get; set; }
}

private static BuyVM GetBuyVM()
{
BuyVM buyVM = new BuyVM();

buyVM.PurchaseDateList = new SelectList(new[] { "Immediately", "1 to 3 months", "4 to 6 months", "More than 6 months" });
buyVM.BedroomsList = new SelectList(new[] { "1", "2", "3", "4", "5+" });
buyVM.StoriesList = new SelectList(new[] { "1", "2", "Does not matter" });

return buyVM;
}

购买.cshtml

    @model Models.BuyVM
// html
@Html.DropDownListFor(m => m.PurchaseDateList, Model.PurchaseDateList, new { @class = "form-control" })

@Html.DropDownListFor(m => m.BedroomsList, Model.BedroomsList, new { @class = "form-control" })

因此,当我返回 View (模型)时,如果存在验证错误 (JQueryVal),在 HTTPPost 中,如果我将模型传递回 View ,我将尝试显示验证错误。但是我有这种类型不匹配。

最佳答案

首先,您的数据模型名称没有意义。名为 BedroomsList 的属性表示 Bedrooms 的集合,但该属性是一个 string。首先为您的属性命名以描述它们是什么,以便其他人可以理解您的代码。

public class BuyModel 
{
public string PurchaseDate { get; set; }
public string Bedrooms { get; set; }
public string Stories { get; set; }
public string SquareFootage { get; set; }
public string PreferredCityLocations { get; set; }
public string AdditionalInfo { get; set; }
}

相应的 View 模型需要包含这些属性以及 SelectList 属性。

public class BuyVM
{
public string PurchaseDate { get; set; }
public string Bedrooms { get; set; }
public string Stories { get; set; }
[Required]
public string SquareFootage { get; set; }
[Required]
public string PreferredCityLocations { get; set; }
public string AdditionalInfo { get; set; }
public SelectList PurchaseDateList { get; set; }
public SelectList BedroomsList { get; set; }
public SelectList StoriesList { get; set; }
}

接下来删除您的 GetBuyVM() 方法并将其替换为 Controller 中填充选择列表的方法,这样您也可以在 ModelState 无效时调用该方法并且您需要返回 View 并将 POST 方法更改为 View 模型的参数(您的 View 基于 BuyVM 因此您必须回发到 BuyVM,而不是 BuyModel)

public ActionResult Buy()
{
BuyVM model = new BuyVM(); // initalise an instance of the view model
ConfigureViewModel(model);
return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Buy(BuyVM model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
Initialize your data model and map the view model properties to it
BuyModel dataModel = new BuyModel()
{
PurchaseDate = model.PurchaseDate,
Bedrooms = model.Bedrooms,
....
};
// save the data model
return View("Success");
}

private ConfigureViewModel(BuyVM model)
{
model.PurchaseDateList = new SelectList(new[] { "Immediately", "1 to 3 months", "4 to 6 months", "More than 6 months" });
model.BedroomsList = new SelectList(new[] { "1", "2", "3", "4", "5+" });
model.StoriesList = new SelectList(new[] { "1", "2", "Does not matter" });
}

最后在 View 中,绑定(bind)到您的属性(PurchaseDate,而不是 PurchaseDateList)

@Html.DropDownListFor(m => m.PurchaseDate, Model.PurchaseDateList)
@Html.DropDownListFor(m => m.Bedrooms, Model.BedroomsList)

关于asp.net-mvc - Asp.Net MVC - 模型绑定(bind)到模型或 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32405538/

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