将View中的值传递给另一个控制器中的操作(使用Viewmodel)

时间:2015-09-22 09:35:39

标签: c# html asp.net-mvc razor

我想将当前视图中的值传递给另一个控制器中的action方法。我使用ViewModel传递两个类的值。但是现在我想从viewmodel类传递一个特定的值作为另一个类中的action方法Create的参数。

创建操作方法

    public ActionResult Create(int id)
    {
        ViewBag.id = id;
        ViewBag.DataFormatID = new SelectList(db.DataFormat, "DataFormatID", "FormatName");
        ViewBag.TCID = new SelectList(db.TC, "TCID", "TCName",id);
        return View();
    }

模型类

public class TC
{
    public int TCID { get; set; }
    public string TCName { get; set; }
    public virtual ICollection<TCSet> TCSets { get; set; }

}

public class TCSet
{
    public int TCSetID { get; set; }
    public string ValueName { get; set; }
  //  public string DataFormat { get; set; }
    public DataUsage DataUsage { get; set; }
    public DataStatus DataStatus { get; set; }
    public int TCID { get; set; }
    public int DataFormatID { get; set; }
    public virtual TC TC { get; set; }
    public virtual DataFormat DataFormat { get; set; }
}

针对特定TCID的ViewTCSet

    public ActionResult ViewTCSet(int ?id)
    {
        var viewmodel = new TC_TCSet();

        if(id!=null)
        {
            ViewBag.TCID = id.Value;
            var tcSet = db.TC.Include(x => x.TCSets).FirstOrDefault(x => x.TCID == id);
            if(tcSet!=null)
            {
                viewmodel.TCSet = tcSet.TCSets;
            }
        }
        return View(viewmodel);
    }

查看ViewTCSet

@model TCImplementation.ViewModels.TC_TCSet
@{
ViewBag.Title = "ViewTCSet";
}
<table>
<tr>
    <th>Tc Set Name</th>
    <th>Data Usage</th>
    <th>Data Status</th>
    <th>Data Format</th>

</tr>
@foreach(var item in Model.TCSet)
{
    <tr>
        <td>@item.ValueName</td>
        <td>@item.DataUsage</td>
        <td>@item.DataStatus</td>
        <td>@item.DataFormat.FormatName</td>
        <td>@Html.ActionLink("Edit", "Edit", "TCSets", new { id= item.TCSetID},null) | @Html.ActionLink("Details", "Details", "TCSets", new { id = item.TCSetID }, null) | @Html.ActionLink("Delete", "Delete", "TCSets", new { id = item.TCSetID }, null)</td>
    </tr>
}
</table>
@Html.ActionLink("Create", "Create", "TCSets", new { id = Model.TCSet }, null)

在此动作链接中,我无法传递值Model.TCSet.TCID 我正在找工作! VINI

0 个答案:

没有答案