如何通过ID为默认另一种观点的价值 [英] How to pass ID to default value of another view
问题描述
我有一个可以有关联的工作事件清单。我对事件和作业的独立意见。从事件索引页,我有以下链接来创建该事件的新作业:
I have a list of incidents which can have associated jobs. I Have a separate views for incidents and jobs. From the incident index page I have the following link to create a new job for that incident:
@Html.ActionLink("Create","Create", "Job", new { id = item.IncidentID }, null)
从该领域采取的事件ID并加载作业视图。我想通过ID为创建新任务的默认值,所以这项工作将被分配事件ID。
which takes the incident ID from that field and loads the Job view. I want to pass the ID as a default value for creating a new job, so the job will be assigned the incident ID.
我做了这个控制器:
public ActionResult Create(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var newid = id;
ViewBag.ActionCode = new SelectList(db.ActionTypes, "ActionCode", "ActionType1");
ViewBag.IncidentID = new SelectList(db.Incidents, "IncidentID", "DefectFreeText");
return View();
}
我怎么能指定一个默认值在表格上的创造就业有何看法?我想是这样的:
How can I assign a default value to the form on the create job view ? I thought something like this:
@Html.EditorFor(model => model.IncidentID, id/newid)
谁能帮我找出我做错了吗?
Can anyone help me figure out what I am doing wrong?
推荐答案
我假设你想设置一个默认项为您事件下拉列表中,当用户传递到您创建操作方法作为查询字符串。我会避免使用ViewBag方法的下拉列表中的数据传输到视图,并切换到一个强类型的视图模型的方法。
I am assuming you want to set a default item for your Incidents dropdown list when user pass that to your create action method as a querystring. I would avoid using ViewBag approach to transfer your dropdown list data to the view and switch to a strongly typed viewmodel approach.
首先创建一个视图模型为我们创建视图。
First create a viewmodel for our create view.
public class CreateJobVM
{
public int IncidentID { set;get;}
public int ActionTypeID { set;get;}
public List<SelectListItem> ActionTypes { set;get;}
public List<SelectListItem> Incidents{ set;get;}
public CreateJobVM()
{
ActionTypes =new List<SelectListItem>();
Incidents=new List<SelectListItem>();
}
}
而在你的GET视图,
And in your GET view,
public ActionResult Create(int? id)
{
var vm=new CreateJobVM();
vm.ActionTypes=GetActionTypes();
vm.Incidents=GetIncidents();
if(id.HasValue)
{
//set the selected item here
vm.IncidentID =id.Value;
}
return View(vm);
}
假设 GetActionTypes
和 GetIncidents
方法返回一个列表 SelectListItems
,从数据库表/ XML /任何地方,你有数据
Assuming GetActionTypes
and GetIncidents
method returns a list of SelectListItems
, From a DB table/ XML /whatever place you have data
public List<SelectListItem> GetActionTypes()
{
List<SelectListItem> actionTypesList = new List<SelectListItem>();
actionTypesList = db.ActionTypes
.Select(s => new SelectListItem { Value = s.ID.ToString(),
Text = s.Name }).ToList();
return actionTypesList;
}
和在你看来这是强类型为CreateJobVM
and in your view which is strongly typed to CreateJobVM
@model CreateJobVM
@using(Html.Beginform())
{
@Html.DropDownListFor(s=>s.ActionTypeID ,Model.ActionTypes)
@Html.DropDownListFor(s=>s.IncidentID,Model.Incidents)
<input type="submit" />
}
当您发布表单,您可以检查的属性值来获得的形式选择的值用户
When you post the form you can check the property values to get the values user selected in the form
[HttpPost]
public ActionResult Create(CreateJobVM model)
{
//check for model.IncidentID, ActionTypeID
// to do : Save and redirect
}
这篇关于如何通过ID为默认另一种观点的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!