我正在使用mvc 5,我在插入下拉列表选择列表时遇到问题
我的观点:
<div class="form-group">
@Html.LabelFor(m => m.NatureofLeave, new { @class = "control-label col-sm-6" })
<div class="col-sm-6 pushleft">
@Html.DropDownListFor(m => m.NatureofLeave, ViewBag.EmployeeLeave as IEnumerable<SelectListItem>, "-Select-")
@Html.ValidationMessageFor(m => m.NatureofLeave, "", new { @style = "color:red;" })
</div>
</div>
我的模特:
[Display(Name = "Type of Leave")]
public List<SelectListItem> NatureofLeave { get; set; }
我的控制器:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Leaves(EmployeeLeave objEmpLeave)
{
objEmpLeave.NatureofLeave = BindDataFromDatabaseToLeave();
var selectitem = objEmpLeave.NatureofLeave.Find(p => p.Value == objEmpLeave.NatureId.ToString());
if (selectitem != null)
{
selectitem.Selected = true;
ViewBag.Message = "NatureofLeave " + selectitem.Text;
}
if (string.IsNullOrEmpty(objEmpLeave.StartDate.ToString()))
{
ModelState.AddModelError("Error", "Please provide start date");
}
else if (string.IsNullOrEmpty(objEmpLeave.EndDate.ToString()))
{
ModelState.AddModelError("Error", "End Date is calculated by startdate... So, make sure start date is not empty!");
}
else if (string.IsNullOrEmpty(objEmpLeave.NatureofLeave.ToString()))
{
ModelState.AddModelError("Error", "Select one");
}
else if (string.IsNullOrEmpty(objEmpLeave.Reason))
{
ModelState.AddModelError("Error", "Reason cannot be empty");
}
else
{
objEmpLeave.UserId = Convert.ToInt32(Session["userId"]);
objEmpLeave.UserName = Convert.ToString(Session["name"]);
int leaveID = objIAccountData.InsertLeave(objEmpLeave);
if (leaveID > 0)
{
TempData["msg"] = "<script>alert('Your Application has been sent')</script>";
}
else
{
TempData["msg"] = "<script>alert('Something went wrong to send the application')</script>";
}
}
return View(objEmpLeave);
}
我正在使用Dapper ORM,所以我会给你存储库。
我的存储库:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyString"].ToString()))
{
var param = new DynamicParameters();
param.Add("@UserId", objEmployeeLeave.UserId);
param.Add("@UserName", objEmployeeLeave.UserName);
param.Add("@StartDate", objEmployeeLeave.StartDate);
param.Add("@EndDate", objEmployeeLeave.EndDate);
param.Add("@LeavesAvailed", objEmployeeLeave.LeavesAvailed);
param.Add("@NatureofLeave", objEmployeeLeave.NatureofLeave);
param.Add("@Reason", objEmployeeLeave.Reason);
param.Add("@UserIdOut", dbType: DbType.Int32, direction: ParameterDirection.Output);
con.Open();
con.Execute("sprocInsertLeaves", param, null, 0, CommandType.StoredProcedure);
int UserIdOut = param.Get<int>("UserIdOut");
con.Close();
return UserIdOut;
}
实际上,问题出在位于下拉列表的存储库中
param.Add("@NatureofLeave", objEmployeeLeave.NatureofLeave);
所以,任何人都可以解决这个问题.... 提前致谢
答案 0 :(得分:0)
基于我的理解
首先,您需要填写NatureofLeave而不是EmployeeLeave
其次,如果您在页面中有Model,则不需要ViewBag
第三,您没有使用EmployeeLeave填充ViewBag
本准则:
return View(objEmpLeave);
正在填充模型而不是ViewBag
为什么不在下面使用如果你在View页面的顶部声明模型,因为模型将是EmployeeLeave:
@Html.DropDownListFor(m => m.NatureofLeave, Model.NatureofLeave, "-Select-")
最好使用:
ConfigurationManager.ConnectionStrings["MyString"].ConnectionString
因为您正在使用:
using (SqlConnection con =
new SqlConnection(
ConfigurationManager.ConnectionStrings["MyString"].ToString()))
根本不需要:
con.Close();
因为dispose将关闭并清理,因为dispose在其内部调用
最后的事情建议:
除非您被迫
,否则尽量不要使用ViewBag用于插入多个数据:
阅读如何使用表格Relation One(Named Parent)=&gt;致许多人(被指名的孩子)
在父级中插入,然后在子级中插入另一个命令