我有一个页面,我在其中创建了某种形式。我有 2 个下拉列表(个人资料列表和薪水列表)。
我在这个表单中有 3 个文本框。我想做的事:
我有 2 个盒子,我在其中创建了一个新的配置文件和一个新的薪水组,并且新的配置文件被添加到 profillist 和薪水组被添加到薪水列表中。
现在我希望禁用第三个框,直到选择了salalilis 和 profillist 中的一个项目。选择项目后,应启用文本框。
我的观点:
@model KUMA.Models.EmployeeCardAdminModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row bgwhite">
<div class="twelve columns">
<h2>Administration KUMA</h2>
<div class="row bgwhite">@using (Html.BeginForm("Index", "Admin"))
{
<div class="three columns">
@Html.DropDownList("UserId", (SelectList)ViewBag.UserId, "--Välj anställd--")
</div>
<div class="three columns" style="margin-right:457px !important;">
@Html.DropDownList("Salaryid", (SelectList)ViewBag.Salaryid, "--Välj LöneGrupp--")
<input style="float:left;" type="submit" value="Knyt" />
</div>
}
@using (Html.BeginForm("Kompetens", "KumaAdmin"))
{
<div class="three columns" style="margin-right: 627px;">
<h6>Kompetenser</h6>
<div style="width:456px;"> @Html.ListBox("kompetensId", (SelectList)ViewBag.KomId, new { placeholder = "Kompetenser" })</div><br/>
@Html.TextBoxFor(mm => mm.Kompetens, new { placeholder = "Ange Kompetens" })
@*@Html.TextBoxFor(mm => mm.KompetensTest, new { placeholder = "Kompetens" })
@Html.TextAreaFor(mm => mm.KompetensTest, new { placeholder = "Kompetens", rows="5", cols="80" })*@
<input type="submit" style="margin-right: 205px;" value="Skapa"/><br/><br/>
</div>
}
@using (Html.BeginForm("Index", "KumaAdmin"))
{
<div class="three columns" style="margin-right: 627px;">
@* <input name="profileTxtBox"type="text" style="width:97px; height:28px;" value="" />*@
@Html.TextBoxFor(mm => mm.Profile, new { placeholder = "Ange Profil" })
@Html.TextBoxFor(mm => mm.SalaryGroup, new { placeholder = "Ange LöneGrupp" })
<input type="submit" value="Skapa"/>
</div>
}
@* <div class="one columns" style=" margin-left: 100px;margin-right: 164px; margin-top: -33px;">
<input name="profileTxtBox"type="submit" style="width:100px;" value="Add" />
</div>*@
<div class="five columns"></div>
</div>
</div
>
控制器:
public class AAdminController : Controller
{
static List<Employee> list = new List<Employee>();
//EmployeeCardAdminModel employee = new EmployeeCardAdminModel();
//
// GET: /Admin/
//[Authorize(Roles = "Admin")]
[HttpGet]
public ActionResult Index()
{
ViewBag.UserId = new SelectList(list, "Id", "Profile");
ViewBag.Salaryid = new SelectList(list, "Id", "SalaryGroup");
ViewBag.KomId = new SelectList(list, "Id", "Kompetens");
ModelState.Clear();
return View("Index");
}
[HttpPost]
// submit for profile & salary box
public ActionResult Index(Models.EmployeeCardAdminModel e)
{
if (string.IsNullOrEmpty(e.Profile) == false && string.IsNullOrEmpty(e.SalaryGroup) == false)
{
// adda a new employye to the list and set the values from the parameter to the model
list.Add(new Employee
{
Id = e.Id + 1,
Profile = e.Profile,
SalaryGroup = e.SalaryGroup
});
}
return (Index());
}
[HttpPost]
// submit for knowledge box
public ActionResult Kompetens(Models.EmployeeCardAdminModel e)
{
if (string.IsNullOrEmpty(e.Kompetens) == false)
{
// adda a new employye to the list and set the values from the parameter to the model
list.Add(new Employee
{
Kompetens = e.Kompetens
});
}
return (Index());
}
最后是我的模型(请注意,emplyee 类与我的模型相同,具有相同的属性,但我读到,为了获得最佳实践,最好将它们分开。):
public class EmployeeCardAdminModel
{
public string Profile { get; set; }
public int Id { get; set; }
public string SalaryGroup { get; set; }
public string Kompetens { get; set; }
}
我通常这样做的方法是调用所需的列表并检查所选索引是否大于空,但问题是我不知道如何以正确的方式从控制器访问列表,所以我可以访问里面的物品。另外我怎样才能在文本框上获得一个ID?我需要这个能够禁用/启用正确的文本框。
我对 mvc 很陌生,我通过做项目来学习,所以所有的建议都值得赞赏。
谢谢!