I have a very simple form. I am collecting Employee Name and DepartmentName from that form as shown below. I also have a hidden input which is named StatusID. Users are not able to see and enter any data in it. However i would like to update this StatusID based on data from DepartmentName input. Let say if DepartmentName is IT then i would like to insert "1" to StatusID row. If the departmentName is HR, StatusID will be updated as "2".
I tried many ways to achieve this but not able to do so.
Any idea how i can achieve it?
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DepartmentName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DepartmentName)
</div>
<div class="editor-field">
@Html.HiddenFor(model => model.StatusID)
</div>
<input type="submit" value="Create" />
</fieldset>
And this is my controller for Create.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
db.Employee.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
And this is my Class:
public partial class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string DepartmentName{ get; set; }
public int StatusID { get; set; }
}
您可以为其创建枚举,并根据部门的状态ID检查枚举条目,可以为此使用转换器
Is there a specific reason that you need to set StatusID in your view, can you not just do it as part of your controller method?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
switch (employee.DepartmentName)
{
case "IT":
employee.StatusID = 1
break;
case "HR":
employee.StatusID = 2
break;
default:
employee.StatusID = 1
break;
}
db.Employee.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
Of course you can change from a switch statement to a ternary operator if you only ever want to check between values of 1 and 2
Thus StatusID
depends on DepartmentName
and this logic belongs to Employee
class, you can set StatusID
when setting DepartmentName
:
private string deparmentName;
public string DepartmentName
{
get { return deparmentName; }
set
{
deparmentName = value;
switch(deparmentName)
{
case "IT" : StatusID = 1; break;
case "HR" : StatusID = 2; break;
default:
StatusID = 0; break;
}
}
}
Keep in mind, that user can input data not only in upper case. Consider also using enum
for departments:
public enum Department
{
None = 0,
IT = 1,
HR = 2
}
And use single property of this enum type instead of two properties for department name and status id:
public partial class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
}
You could also define your own ModelBinder to map those properties the way you want :
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
object model = bindingContext.Model;
Employee employee = (Employee)model;
if (employee == null)
{
employee = new Employee();
}
string departnementName = "DepartmentName";
var departnementNameValue = bindingContext.ValueProvider.GetValue(departnementName);
try
{
if (departnementName == "IT")
{
employee.StatusID = 1;
}
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
bindingContext.ModelState.SetModelValue(departnementName, departnementNameValue);
}
return employee;
}
If else or switch statement?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
if(employee.DepartmentName == "IT")
{
employee.StatusID == 1;
}
else if(employee.DepartmentName == "HR")
{
employee.StatusID == 2;
}
else
{
employee.StatusID == 1;
}
db.Employee.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.