我有一个DropDownList,它显示状态列表,但是当我从DropDownlist中选择一个项目,然后检查HTML标记时,我可以看到没有选定的属性,然后我进行了研究,发现我需要在Controller和比我尝试在Controller中实现它的方法要多,但是我遇到了一些错误:)就像通常我在Razor View(static)中实现DropDown一样,但这是第一次:)我想在Controller中实现它,使它变得动态。
谁能指出我正确的方向:)
在此先感谢:)
控制器:
//DropDown
public List<VMRMA.NameStatusDropDown> GetStatusForDropDown()
{
List<VMRMA.NameStatusDropDown> result = new List<VMRMA.NameStatusDropDown>();
var obj = db.RMAStatus.Select(u => u).ToList();
if (obj != null && obj.Count() > 0)
{
foreach (var data in obj)
{
VMRMA.NameStatusDropDown model = new VMRMA.NameStatusDropDown();
model.Status = data.Status;
model.ID = data.ID;
result.Add(model);
}
}
return result;
}
//Dropdown runs in this Action
public ActionResult RMA ()
{
VMRMA model = new VMRMA();
model.NameStatusDropDowns = GetStatusForDropDown();
//RMA query and some an other stuff
return View(model);
}
ViewModel:
public class VMRMA
{
public List<NameStatusDropDown> NameStatusDropDowns { get; set; }
//DropDown
public class NameStatusDropDown
{
public NameStatusDropDown()
{
}
public NameStatusDropDown(int ID, string Status)
{
this.ID = ID;
this.Status = Status;
}
public int ID { get; set; }
public string Status { get; set; }
}
}
查看:
@using ModelNamespace.Models
@model VMRMA
<form>
<div class="form-group">
<label class="form-control-label">Select a status</label>
<br />
<select>
<option>Select</option>
@foreach (var item in Model.NameStatusDropDowns)
{
<option value="@item.ID">@item.Status</option>
}
</select>
</div>
<div class="form-group">
<input type="submit" value="Send data" class="btn btn-primary">
</div>
</form>
HTML标记:
<div class="form-group">
<label class="form-control-label">Select a status</label>
<br>
<select>
<option>Select</option>
<option value="1">Sendt</option>
<option value="2">Under behandling</option>
<option value="3">Blive behandlet</option>
<option value="4">Modtaget</option>
</select>
</div>
答案 0 :(得分:2)
这两个帖子帮助我解决了这个问题,感谢@Stephen Muecke撰写了Here的好帖子,也感谢此帖子带有Here的出色解释。
这就是我所做的,也许有一天它对某人有所帮助:):
添加到属性到我的视图模型:
public class VMRMA
{
public List<SelectListItem> Status { set; get; }
public int? SelectedStatus { set; get; }
}
将我的ActionResult更改为:
public ActionResult RMA (int Id)
{
VMRMA model = new VMRMA();
model.Status = new SelectList(DatabaseNameSpace.RMAStatus, "ID",
"Status").ToList();
//some an other stuff
return View(model);
}
,然后将我的视图更改为:
@Html.DropDownListFor(s => s.SelectedStatus, Model.Status, "- Select -", new { @class = "form-control" })
答案 1 :(得分:0)
控制器:
ViewBag.Statuses= new SelectList(_context.RMAStatus
.Select(item => new { value = item.Id, text = item.Status}), "value", "text", selectedId);
查看:
@Html.DropDownListFor(x => x.StatusId, ViewBag.Statuses as SelectList, "- please select -")
答案 2 :(得分:0)
以此创建局部视图:
@model MyApp.Models.MyClass
@{
Layout = null;
}
@*@Html.Partial("ActionMethod", "Controller", new ViewDataDictionary { { "Name", "TestName" } })*@
@Html.DropDownList((String)TempData["Name"], new SelectList( ViewBag.Specialities,"Value","Text"),
new { @class = "form-control", @multiple="multiple" });
然后在您的控制器中
List<MyClass> lstSpecialities =
ViewBag.Specialities = lstSpecialities; // Now it is available for the view
最后一步,使用@Html.RenderAction()