Eduard
Eduard

Reputation: 722

Pass HiddenFor ID from View to a another ActionResult which is a PartialView

My title I think is a little confusing, let me explain:

I have an IndexView where I display all the Jobs of Companies and I am trying to add a PartialView with the coresponding Image.

I do not want to save the Image on the JobTable when I submit a Job. I want Images to be separate and linked only by UserID. In the page details I had an int? ID in the ActionResult header, I made two LINQ queries, first selects UserID with the int? ID, in second query I selected the ImageID with UserID from first query so I am trying to do the same but here I have no ID in URL because I display a list.

Since this is a list I tried to get the value from each @Html.HiddenFor(model => item.ID) which is inside a foreach but I couldn't pass it back to my ActionResult (which is PartialView not Index), I tried with ViewBag, Sessions everything is null everytime. I gave it a name, a Value(uppercase and lowercase) with @ and without.

Here code:

    public ActionResult ImgPanel(Job job, int? ID) 
    {
        var JobUserID = (from j in db.Jobs
                        where j.ID == ID /*Here should be the ID from the View of each job object*/
                        select j.UserID).FirstOrDefault();

        var img = (from j in db.Images
                    where j.UserID == JobUserID
                    select j).ToList();

        return View(img);          

    }

Well the ActionResult for ImgPanel in this state returns an Image, but only first(in all Jobs same Image), the second time my breakpoints are telling me only null on everything.

View Code:

@foreach (var item in Model)
{      
    @Html.HiddenFor(model => item.ID) /*I removed all tries with name Value Viewbags etc*/
    @Html.Action("ImgPanel", "Jobs") /*Here I call the PartialView ImgPanel*/
    <div class="row">
        <div class="col-sm-6 col-md-4">
            <div class="thumbnail">
                <h3>@Html.DisplayFor(modelItem => item.ApplicationUsers.CompName)</h3>
                <div class="caption">
                    <h4>@Html.DisplayFor(modelItem => item.Name)</h4>                   
                    <h5><strong>Days Left: </strong>@Convert.ToInt32((item.DateExp - DateTime.Now).TotalDays)</h5>
                    <p>
                        <a href="@Url.Action("Details", "Jobs", new { Id = item.ID})" class="btn btn-info" role="button">Details</a>
                        <a href="#" class="btn btn-success" role="button">Bookmark</a>
                        <a href="#" class="btn btn-danger" role="button">Hide</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
}

Upvotes: 0

Views: 272

Answers (1)

Ricardo Reyna
Ricardo Reyna

Reputation: 594

Try passing in a viewmodel which contains your IDs.

In your view:

@Html.Action("ImgPanel", "Jobs", new MyViewModel() { ItemId = item.ID })

In your controller:

public ActionResult ImgPanel(MyViewModel viewmodel) 
{
    var JobUserID = (from j in db.Jobs
                    where j.ID == viewmodel.ItemId /*Here you use the ID stored in the viewmodel*/
                    select j.UserID).FirstOrDefault();

    var img = (from j in db.Images
                where j.UserID == JobUserID
                select j).ToList();

    return View(img);          

}

Hope that helps!

Upvotes: 1

Related Questions