C# 已选择},函数(数据){ $(“#目标”).html(数据); }); }); }); @{ RenderAction(“InhoudByIdPartial”,Model.EventID); } 贝里希滕酒店 @Html.DropDownList(“EventBerichten”,new SelectList(ViewBag.EventBerichten,“EventBerichtenID”,“Titel”),new{@class=“form control”,onchange=“$(this.form).submit();”)
谢谢-这有助于我更好地理解和解决我遇到的问题。 为获取selectedItem的文本而提供的JQuery对我来说不起作用 我把它改成了C# 已选择},函数(数据){ $(“#目标”).html(数据); }); }); }); @{ RenderAction(“InhoudByIdPartial”,Model.EventID); } 贝里希滕酒店 @Html.DropDownList(“EventBerichten”,new SelectList(ViewBag.EventBerichten,“EventBerichtenID”,“Titel”),new{@class=“form control”,onchange=“$(this.form).submit();”),c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,谢谢-这有助于我更好地理解和解决我遇到的问题。 为获取selectedItem的文本而提供的JQuery对我来说不起作用 我把它改成了 $(function () { $("#SelectedVender").on("change", function () { $("#SelectedvendorText").val($(**"#SelectedVender option:selected"**).text()); }); }); <div class="form
$(function () {
$("#SelectedVender").on("change", function () {
$("#SelectedvendorText").val($(**"#SelectedVender option:selected"**).text());
});
});
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
如果要使用
@Html.DropDownList
,请遵循以下步骤
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
控制器:
var categoryList = context.Categories.Select(c => c.CategoryName).ToList();
ViewBag.CategoryList = categoryList;
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
视图:
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
MVC 5/6/Razor页面
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
我认为最好的方法是使用强类型模型,因为ViewBag已经被使用得太多了:)
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
MVC 5示例
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
你的行动
public async Task<ActionResult> Register()
{
var model = new RegistrationViewModel
{
Roles = GetRoles()
};
return View(model);
}
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
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);
}
公共异步任务寄存器()
{
var模型=新注册视图模型
{
Roles=GetRoles()
};
返回视图(模型);
}
您的视图模型
public class RegistrationViewModel
{
public string Name { get; set; }
public int? RoleId { get; set; }
public List<SelectListItem> Roles { get; set; }
}
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
公共类注册视图模型
{
公共字符串名称{get;set;}
公共int?RoleId{get;set;}
公共列表角色{get;set;}
}
你的观点
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
@LabelFor(model=>model.RoleId,htmlAttributes:new{@class=“col form label”})
@DropDownListFor(model=>model.RoleId,model.Roles,“--Select Role--”,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.RoleId,“,new{@class=“text danger”})
你的后期行动
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
[HttpPost,ValidateAntiForgeryToken]
公共异步任务寄存器(RegistrationViewModel模型)
{
if(ModelState.IsValid)
{
var_roleId=model.roleId,
MVC 6将会有点不同
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
采取行动
public async Task<ActionResult> Register()
{
var model = new RegistrationViewModel
{
Roles = GetRoles()
};
return View(model);
}
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
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);
}
公共异步任务寄存器()
{
var_roles=新列表();
_角色。添加(新建SelectListItem
{
Text=“选择”,
Value=“”
});
foreach(GetRoles()中的var角色)
{
_角色。添加(新建SelectListItem
{
Text=z.名称,
值=z.Id
});
}
var模型=新注册视图模型
{
角色=\u角色
};
返回视图(模型);
}
您的视图模型将与MVC 5相同
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
您的视图将如下所示
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
<select asp-for="RoleId" asp-items="Model.Roles"></select>
帖子也将相同
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
剃须刀页面
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
您的页面模型
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
[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>
[BindProperty]
公共int用户用户{get;set;}=1;
公共列表角色{get;set;}
公共互联网
{
角色=新列表{
新建SelectListItem{Value=“1”,Text=“X”},
新建SelectListItem{Value=“2”,Text=“Y”},
新建SelectListItem{Value=“3”,Text=“Z”},
};
}
选择角色
我希望它能帮助一些人:)简单的解决方案不确定是否有人提出过这样的建议。这也可能对某些事情不起作用。尽管如此,这是下面的简单解决方案
<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>
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegistrationViewModel model)
{
if (ModelState.IsValid)
{
var _roleId = model.RoleId,
new SelectListItem { Value = "1", Text = "Waiting Invoices", Selected = true}
List<SelectListItem> InvoiceStatusDD = new List<SelectListItem>();
InvoiceStatusDD.Add(new SelectListItem { Value = "0", Text = "All Invoices" });
InvoiceStatusDD.Add(new SelectListItem { Value = "1", Text = "Waiting Invoices", Selected = true});
InvoiceStatusDD.Add(new SelectListItem { Value = "7", Text = "Client Approved Invoices" });
@Html.DropDownList("InvoiceStatus", InvoiceStatusDD)
您已将下拉列表绑定到属性
ddlventor
,但您的模型中不存在此类属性。请添加属性int SelectedVendor
或类似于您的模型并绑定到该属性。@Raghubar第二种方法更受欢迎,因此请使用模型绑定确定。另外,请告诉我如何在dropdownlist中设置所选项。@Raghubar,只需在控制器中设置属性SelectVender
的值,以便与post操作中的一个选项的值相匹配:MV.Vendor=new SelectList(db.Usp\u VendorList(),“VendorId”,“VendorName”,VM.SelectedVendor);
最后一个参数用于设置选定的value@EhsanSajjad如何获得所选文本?@Raghubar-这不是为你…或任何人…我只是想知道原因,以便我可以改进…@PranayRana,取消了它-但必须承认,在你编辑之前,我将标记为关闭,作为一个链接唯一的答案-也许我感觉相同“IEnumerable”不包含“Gender”的定义,并且没有接受第一个参数的扩展方法“Gender”type@Html.DropDownListFor(m=>m.Gender,Namespacex.Models.Movie.GetGenderSelectItems())明白了。我有@model IEnumerable。这是IEnumerable。如何将代码传递给IEnumerable?