How to pass ID to default value of another view

♀尐吖头ヾ 提交于 2020-01-26 03:27:10

问题


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)

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.

I made this controller:

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?


回答1:


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>();
  }
}

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);
}

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;
}

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
}



回答2:


Just pass it with the help of ViewBag/ViewData

ViewBag.DefaultId=id;

return View();

If you have a model based view

YourViewModel.DefaultId=id;

return View(YourViewModel);

In View,

@ViewBag.DefaultId   

or with ViewModel

YourViewModel.DefaultId


来源:https://stackoverflow.com/questions/22017613/how-to-pass-id-to-default-value-of-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!