我想从任务名称的下拉列表中选择特定名称,然后将其传递给控制器
这是我的观点
@model MVCSecurity.Models.AssignSubTask
<table>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SubTaskModel</h4>
<hr />
<tr>
<td>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.SubTaskName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SubTaskName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SubTaskName, "", new { @class = "text-danger" })
</div>
</div>
</td>
<td> @Html.ActionLink("Create Subtask", "SubTaskCreate", "Account", null, new { style = "color:aqua" }) </td>
</tr>
<div class="form-group">
Tasks:
<div class="col-md-10">
@Html.DropDownListFor(m => m.TaskList, new SelectList(Model.TaskList, "Value", "Text"), new { style = "width:200px", @class = "form-control" })
@Html.ValidationMessageFor(model => model.TaskList)
</div>
</div>
<tr>
<td>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" />
</div>
</div>
</td>
</tr>
</div>
}
</table>
<div style="color:red">@ViewBag.msg</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是我的控制人
[HttpGet]
public ActionResult SubTaskCreate()
{
AssignSubTask subTaskModel = new AssignSubTask();
subTaskModel.TaskList = GetAll_Tasks();
return View(subTaskModel);
}
[HttpPost]
public ActionResult SubTaskCreate(SubTaskModel subTask)
{
InsertSubTask(subTask);
ViewBag.msg = "Subtask created Successfully";
AssignSubTask subTaskModel = new AssignSubTask();
subTaskModel.TaskList = GetAll_Tasks();
return View(subTask);
}
在这些方法中,我想将任务列表中的特定任务名称插入表中
public void InsertSubTask(SubTaskModel sub)
{
AssignSubTask subTaskModel = new AssignSubTask();
subTaskModel.TaskList = GetAll_Tasks();
try
{
string Connectionstring = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(Connectionstring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
string query = "Insert into SubTask(SubTaskName,TaskId)values(@subtaskname,(select TaskId from Task where Taskname='" + subTaskModel.TaskName + "' ))";
cmd.Connection = con;
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@subtaskname", ParameterDirection.Input).Value = sub.SubTaskName;
cmd.Parameters.AddWithValue("@TaskName", ParameterDirection.Input).Value = sub.TaskName;
cmd.Parameters.AddWithValue("@TaskId", ParameterDirection.Input).Value = sub.TaskId;
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
这是我的模特
[Table("SubTask")]
public class SubTaskModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SubTaskId { get; set; }
[Required]
public string SubTaskName { get; set; }
public int TaskId { get; set; }
public string TaskName { get; set; }
[ForeignKey("TaskId")]
public virtual TaskModel taskmodel { get; set; }
[ForeignKey("SubTaskId")]
public virtual SubTaskModel HasSubTask { get; set; }
}
public class AssignSubTask
{
public string SubTaskName { get; set; }
public string TaskName { get; set; }
public string UserID { get; set; }
public List<SelectListItem> TaskList { get; set; }
public List<SelectListItem> SubTaskList { get; set; }
}
我该怎么做? 我想从任务名称的下拉列表中选择特定名称,然后将其传递给控制器
答案 0 :(得分:1)
DropDownListFor
的第一个参数是lambda表达式,您必须提供一个表达式,该表达式指示当前模型中的特定属性以进行渲染。它不能是您的数据源属性。
@Html.DropDownListFor(m => m.TaskName, new SelectList(Model.TaskList, "Value", "Text"), new { style = "width:200px", @class = "form-control" })