Rag*_*bar 44 c# asp.net asp.net-mvc asp.net-mvc-4
我有下拉列表,我已从数据库中填写.现在我需要在Controller中获取所选值进行一些操作.但没有得到这个想法.我试过的代码.
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public ActionResult ShowAllMobileDetails()
{
MobileViewModel MV = new MobileViewModel();
MV.MobileList = db.Usp_InsertUpdateDelete(null, "", "", null, "", 4, MergeOption.AppendOnly).ToList();
MV.Vendor = new SelectList(db.Usp_VendorList(), "VendorId", "VendorName");
return View(MV);
}
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
string strDDLValue = ""; // Here i need the dropdownlist value
return View(MV);
}
Run Code Online (Sandbox Code Playgroud)
<table>
<tr>
<td>Mobile Manufacured</td>
<td>@Html.DropDownList("ddlVendor", Model.Vendor, "Select Manufacurer") </td>
</tr>
<tr>
<td>
</td>
<td>
<input id="Submit1" type="submit" value="search" />
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
Ehs*_*jad 85
您可以从Request
使用中读取它Request.Form
,您的下拉名称是formCollection中的ddlVendor
pass ddlVendor
密钥,以获取由表单发布的值:
string strDDLValue = Request.Form["ddlVendor"].ToString();
Run Code Online (Sandbox Code Playgroud)
或使用FormCollection
:
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV,FormCollection form)
{
string strDDLValue = form["ddlVendor"].ToString();
return View(MV);
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用模型绑定,那么在Model中添加一个属性:
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
public string SelectedVendor {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
在视图中:
@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
Run Code Online (Sandbox Code Playgroud)
并在行动:
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
string SelectedValue = MV.SelectedVendor;
return View(MV);
}
Run Code Online (Sandbox Code Playgroud)
如果您还要发布所选项目的文本,则必须在隐藏字段中添加隐藏字段和下拉选择更改集选定项目文本:
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
public string SelectVendor {get;set;}
public string SelectedvendorText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用jquery设置隐藏字段:
<script type="text/javascript">
$(function(){
$("#SelectedVendor").on("change", function {
$("#SelectedvendorText").val($(this).text());
});
});
</script>
@Html.DropDownListFor(m=>m.SelectedVendor , Model.Vendor, "Select Manufacurer")
@Html.HiddenFor(m=>m.SelectedvendorText)
Run Code Online (Sandbox Code Playgroud)
And*_*rei 11
非常基本的性别领域模型.GetGenderSelectItems()
返回填充DropDownList所需的选择项.
public enum Gender
{
Male, Female
}
public class MyModel
{
public Gender Gender { get; set; }
public static IEnumerable<SelectListItem> GetGenderSelectItems()
{
yield return new SelectListItem { Text = "Male", Value = "Male" };
yield return new SelectListItem { Text = "Female", Value = "Female" };
}
}
Run Code Online (Sandbox Code Playgroud)
请确保您将自己包裹@Html.DropDownListFor
在表格标签中.
@model MyModel
@using (Html.BeginForm("MyController", "MyAction", FormMethod.Post)
{
@Html.DropDownListFor(m => m.Gender, MyModel.GetGenderSelectItems())
<input type="submit" value="Send" />
}
Run Code Online (Sandbox Code Playgroud)
您的.cshtml Razor视图名称应与控制器操作名称相同,文件夹名称应与控制器名称匹配,例如Views\MyController\MyAction.cshtml
.
public class MyController : Controller
{
public ActionResult MyAction()
{
// shows your form when you load the page
return View();
}
[HttpPost]
public ActionResult MyAction(MyModel model)
{
// the value is received in the controller.
var selectedGender = model.Gender;
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们强类型和枚举独立:
var genderSelectItems = Enum.GetValues(typeof(Gender))
.Cast<string>()
.Select(genderString => new SelectListItem
{
Text = genderString,
Value = genderString,
}).AsEnumerable();
Run Code Online (Sandbox Code Playgroud)
MVC 5/6/剃刀页面
我认为最好的方法是使用强类型模型,因为 Viewbags 已经被滥用了:)
MVC 5示例
您的获取行动
public async Task<ActionResult> Register()
{
var model = new RegistrationViewModel
{
Roles = GetRoles()
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
你的视图模型
public class RegistrationViewModel
{
public string Name { get; set; }
public int? RoleId { get; set; }
public List<SelectListItem> Roles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您的看法
<div class="form-group">
@Html.LabelFor(model => model.RoleId, htmlAttributes: new { @class = "col-form-label" })
<div class="col-form-txt">
@Html.DropDownListFor(model => model.RoleId, Model.Roles, "--Select Role--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RoleId, "", new { @class = "text-danger" })
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
您的帖子操作
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
Run Code Online (Sandbox Code Playgroud)
MVC 6 会有一点不同
采取行动
public async Task<ActionResult> Register()
{
var _roles = new List<SelectListItem>();
_roles.Add(new SelectListItem
{
Text = "Select",
Value = ""
});
foreach (var role in GetRoles())
{
_roles.Add(new SelectListItem
{
Text = z.Name,
Value = z.Id
});
}
var model = new RegistrationViewModel
{
Roles = _roles
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
您的视图模型将与 MVC 5 相同
你的视图会像
<select asp-for="RoleId" asp-items="Model.Roles"></select>
Run Code Online (Sandbox Code Playgroud)
帖子也将是相同的
剃刀页面
您的页面模型
[BindProperty]
public int User User { get; set; } = 1;
public List<SelectListItem> Roles { get; set; }
public void OnGet()
{
Roles = new List<SelectListItem> {
new SelectListItem { Value = "1", Text = "X" },
new SelectListItem { Value = "2", Text = "Y" },
new SelectListItem { Value = "3", Text = "Z" },
};
}
<select asp-for="User" asp-items="Model.Roles">
<option value="">Select Role</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我希望它可以帮助别人:)