gpt4 book ai didi

c# - ASP.NET MVC 使用强类型 ViewModel 将数据从 View 保留到 Controller

转载 作者:行者123 更新时间:2023-11-30 21:55:51 25 4
gpt4 key购买 nike

对于提出一个之前已经回答过的问题,我提前表示歉意,但我仍然是一个新手,发现很难将解决方案应用于我自己的问题。

我有一个 View 模型,我在创建时填充了一些数据,然后打算使用级联下拉列表填充数据(我不能使用任何客户端脚本)

public class StudentPortalModel
{
public SelectList LocationListModel { get; set; }
public SelectList ResidenceListModel { get; set; }
public int ChosenLocation { get; set; }
public int ChosenBlock { get; set; }
public int ChosenFlat { get; set; }
public int ChosenRoom { get; set; }
public int ChosenResidence { get; set; }
}

我在 Controller 中有一个操作,它使用一些列表填充 View 模型,我将这些列表用于下拉菜单:

[HttpGet]
public ActionResult SelectResidence()
{


List<Location> objLocationList = (from data in db.Locations select data).ToList();
SelectList objLocationModelData = new SelectList(objLocationList, "LocationId", "LocationName", 0);


List<Residence> objResidenceList = (from data in db.Residences select data).ToList();
SelectList objResidenceModelData = new SelectList(objResidenceList, "ResidenceId", "BlockNumber", 0);


StudentPortalModel objStudentPortalModel = new StudentPortalModel();
objStudentPortalModel.LocationListModel = objLocationModelData;
objStudentPortalModel.ResidenceListModel = objResidenceModelData;


return View(objStudentPortalModel);
}

我希望将发送到 View 的模型数据返回给 Controller

 @using (Html.BeginForm("SelectLocation", "StudentPortal", FormMethod.Post))
{
@Html.DropDownListFor(x => x.ChosenLocation, Model.LocationListModel, "---Select Location---")

<input type="submit" value="Submit"/>
}

我希望在发送回 HttpGet 方法时保留我发布到 View 的所有数据。

[HttpPost]
public ActionResult SelectLocation(StudentPortalModel testPortalInstance)
{
List<Location> objLocationList = (from data in db.Locations select data).ToList();
SelectList objLocationModelData = new SelectList(objLocationList, "LocationId", "LocationName", 0);


List<Residence> objResidenceList = (from data in db.Residences select data).Where(i => i.LocationId == testPortalInstance.ChosenLocation).DistinctBy(x => x.BlockNumber).ToList();
SelectList objResidenceModelData = new SelectList(objResidenceList, "ResidenceId", "BlockNumber", 0);

testPortalInstance.LocationListModel = objLocationModelData;
testPortalInstance.ResidenceListModel = objResidenceModelData;

return View("SelectResidence", testPortalInstance);

}

你们可能都知道,唯一被发回的数据是 ChosenLocation,因为当最终用户从下拉列表中选择它时,它被发回,其他一切都是空的

本质上,我需要一种方法来保留用户为每个下拉列表选择的选项。这使我能够缩小用户希望选择的房间的范围,直到我能够从表格中破译一个房间并将其分配给用户。

我最大的问题是我似乎无法找到发送到 View 的 View 模型数据并将其分配给相同的值以发回。当我发回 Controller 时,一切都是空的,这意味着我必须重新创建数据。

我为我的胡言乱语道歉,我把它归结为我只编程了一个月。

提前感谢您的帮助!

最佳答案

不太清楚为什么您不想使用 javascript/jquery(它会提供更好的性能),但您需要将选定的值从相关的 POST 方法传递给每个 GET 方法。假设第一步是选择一个位置,然后是一个住所,您的 Controller 方法将是

public ActionResult SelectResidence()
{
List<Location> locations = (from data in db.Locations select data).ToList();
StudentPortalModel model = new StudentPortalModel()
{
LocationListModel = new SelectList(locations, "LocationId", "LocationName")
}
return View(model);
}

请注意,为 ResidenceListModel 生成 SelectList 没有意义,因为它不会在第一个 View 中使用

@model StudentPortalModel
@using (Html.BeginForm()) // no need to add parameters
{
@Html.DropDownListFor(x => x.ChosenLocation, Model.LocationListModel, "---Select Location---")
<input type="submit" value="Submit"/>
}

将返回到

[HttpPost]
public ActionResult SelectResidence(StudentPortalModel model)
{
if (!ModelState.IsValid)
{
// repopulate locations and return view
}
return RedirectToAction("SelectResidence", new { locationID = model.ChosenLocation });
}

将重定向到

public ActionResult SelectResidence(int locationID)
{
List<Residence> residences = (from data in db.Residences select data).Where(i => i.LocationId == locationID).ToList();
StudentPortalModel model = new StudentPortalModel()
{
ChosenLocation = locationID,
ResidenceListModel = new SelectList(residences, "ResidenceId", "BlockNumber")
}
return View(model);
}

谁的观点

@model StudentPortalModel
@using (Html.BeginForm())
{
@Html.HiddenFor(x => x.ChosenLocation)
@Html.DropDownListFor(x => x.ChosenResidence, Model.ResidenceListModel , "---Select Residence---")
<input type="submit" value="Submit"/>
}

将返回到

public ActionResult SelectResidence(StudentPortalModel model)
{
if (!ModelState.IsValid)
{
// repopulate residences based on the value of model.ChosenLocation and return view
}
// your model now contains values for both ChosenLocation and ChosenResidence
}

类似地,如果您随后需要转到一个 View 以选择一个房间,您将重定向到(例如)public ActionResult SelectResidence(int locationID, int residenceID)

关于c# - ASP.NET MVC 使用强类型 ViewModel 将数据从 View 保留到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865562/

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