我正在编写一个asp.Net MVC应用程序,但我遇到了问题。我需要两个控件 - 一个是DropDownList,另一个是Button。
剃刀/ HTML:
c = (char)(c + 10)
CompanyController:
<h3 class="text-center margin">Parking Spaces in <strong>@Model.Company.Name</strong></h3>
@Html.DropDownListFor(r => r.UnassignedParkingSpaces, new SelectList(Model.UnassignedParkingSpaces, "Id", "Id"), new { @class = "form-control" })
<a class="btn btn-primary btn-block btn-sm button-width" onclick="location.href = '@Url.Action("Assign", "Company", new {buildingGuid = Model.BuildingGuid, companyGuid = Model.Company.Guid, parkingSpaceGuid = })'">
<i class="fa fa-exchange"></i> @ResourcesGeneral.AssignButton
</a>
我在Html中使用的ViewController:
public ActionResult Assign(Guid parkingSpaceGuid, Guid companyGuid, Guid buildingGuid)
{
var building = _buildingReader.GetBuilding(buildingGuid);
building.AssignParkingSpace(parkingSpaceGuid, companyGuid);
return RedirectToAction("DisplayParkingSpaces", new { buildingGuid, companyGuid});
}
停车位模型:
public class CompanyParkingSpacesViewModel
{
public Guid BuildingGuid { get; set; }
public AllParkingSpacesListViewModel AllParkingSpacesListViewModel { get; set ; }
public Company Company { get; set; }
public List<ParkingSpace> CompanyParkingSpaces { get; set; }
public IEnumerable<ParkingSpace> UnassignedParkingSpaces { get; set; }
}
所以我需要传递给我在DropDownList中选择的按钮元素somy控制器具有所有参数。你怎么能这样做?
答案 0 :(得分:0)
您需要将输入控件(SELECT元素)保存在表单中并提交表单。
您还使用DropDownListFor方法不正确。要显示下拉列表,您不需要使用ParkingSpace
实体集合作为类型。您只需使用List<SelectListItem>
类型即可。您还需要为所选选项值添加另一个属性。
public class CompanyParkingSpacesViewModel
{
public Guid BuildingGuid { get; set; }
public AllParkingSpacesListViewModel AllParkingSpacesListViewModel { get; set ; }
public Company Company { get; set; }
public List<ParkingSpace> CompanyParkingSpaces { get; set; }
public IEnumerable<SelectListItem> UnassignedParkingSpaces { get; set; }
public Guid SelectedParkingSpace { set;get;}
}
确保您正在加载GET操作中的数据
public ActionResult Assign()
{
var vm = new CompanyParkingSpacesViewModel();
vm.UnassignedParkingSpace = db.ParkingSpaces
.Select(x=>new SelectListItem { Value = x.Guid.ToString(),
Text=x.DescriptionHint }).ToList();
//Assign other properties as well
vm.Company=new Company();
return View(vm);
}
现在以你的形式。
@model CompanyParkingSpacesViewModel
@using(Hml.BginForm("Assign","YourControllerName"))
{
<label>Select a parking space </label>
@Html.DropDownListFor(r => r.SelectedParkingSpace,
Model.UnassignedParkingSpace,new { @class = "form-control" })
<input type="submit" value="Assign" />
}
确保您的操作方法参数名称与SELECT元素名称属性值匹配。
public ActionResult Assign(Guid SelectedParkingSpace, Guid companyGuid,
Guid buildingGuid)
{
var building = _buildingReader.GetBuilding(buildingGuid);
building.AssignParkingSpace(SelectedParkingSpace, companyGuid);
return RedirectToAction("DisplayParkingSpaces", new { buildingGuid, companyGuid});
}
由于您的Assign
操作方法代码需要buildingGuid
和companyGuid
,因此您需要将这些代码保留在表单中,以便在表单格式化时将其映射到方法参数提交。您可以将其保留在隐藏的输入字段中。
@using(Hml.BginForm("Assign","YourControllerName")
{
<label>Select a parking space </label>
@Html.DropDownListFor(r => r.SelectedParkingSpace,
Model.UnassignedParkingSpace,new { @class = "form-control" })
<input type="hidden" name="companyGuid" value="@Model.Company.Guid" />
<input type="hidden" name="buildingGuid" value="@Model.BuildingGuid" />
<input type="submit" value="Assign" />
}