none
Pass Value from View to a different controller RRS feed

  • Question

  • I want to pass a value from my current view to action method in another controller. I have used ViewModel to pass the values of both the classes. But now I want to pass a particular value from the viewmodel class as a parameter for the action method Create  in another class.


    **Create Action method**

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



     **model classes**

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


     **ViewTCSet for a particular TCID**

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



    View for 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)



    In this actionlink I am unable to pass the value `Model.TCSet.TCID`
    I am looking for a work around!
    Vini
    • Edited by vini1234 Tuesday, September 22, 2015 11:02 AM Code not displayed properly
    Tuesday, September 22, 2015 11:00 AM

Answers

  • Hi,

    Please use the following code:

    >@Html.ActionLink("Edit", "Edit", "TCSets",null, new { id= item.TCSetID})


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Pengzhen Song Tuesday, October 6, 2015 8:46 AM
    Wednesday, September 23, 2015 6:24 AM