如何设置从模型中读取数据的create方法的属性?

时间:2014-08-04 11:48:56

标签: asp.net-mvc asp.net-mvc-5 repository-pattern

我有两个表,其中一个是Request表和Project表。当你转到请求列表(索引)时,每个记录旁边都有一个名为Convert的链接,当你导航它时,该记录中的一些值必须设置为创建项目的视图,如RequestDate = start_date和close_date = end_date。

项目控制器

public ActionResult Convert(int ID)
        {
            var model = new AddProjectView();
            model.ReqNumber = ID;
            ViewBag.ProjectType = new SelectList(db.ProjectTypes, "Type", "Type");
            ViewBag.Size = new SelectList(db.ProjectSizes, "Size", "Size");
            ViewBag.Status = new SelectList(db.ProjectStatuses, "Status", "Status");
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Convert(AddProjectView model)
        {
            var bus = new ProjectBusiness();
            bus.Insert(model);

            return RedirectToAction("Index");
        }

请求查看索引

这是我将导航转换链接,然后它应该引导我使用我需要的值的项目操作方法,使用请求编号(ReqNumber)获取它们。

@model IEnumerable<Request> // assumed this is what the model is
foreach(var request in Model)
{
  .... // Display some property(s) of request
  @Html.ActionLink("Convert", "Project", new { ID = request.ReqNumber}) // pass the ID of the request to the controller
}

1 个答案:

答案 0 :(得分:0)

试试这个:

foreach(var request in Model)
{  
    @Html.ActionLink("Convert", "Convert", "MyControllerName", new { id = request.ReqNumber }, null)
}

和你的行动:

[HttpGet]
public virtual ActionResult Convert(int? id)
{
    return View();
}