user2915962
user2915962

Reputation: 2711

Pass object to partial view

I have this controller which fetches an object that I would like to render in a partial view:

public ActionResult EditPhoto(string id)
{
    var photo = RavenSession.Load<ContentPage>(id);
    return View("_editPhoto");
}

Its the photo I would like to pass to my partial view. I have this in my view:

@{
    Html.RenderPartial("_editPhoto" );
}

How will I go about to pass my photo into the partial view in order for it to render in its parent?

EDIT: This is how I pass the object to the controller:

@foreach (var item in Model.Photographys)
{
    <li class="span3" style="text-align: center">

        <div class="thumbnail thumbnail-1">
            @Html.ActionLink(item.Name,
                "EditPhoto",   // <-- ActionMethod
                new { id = item.Id }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                //     otherwise you call the WRONG method ...
                //     (refer to comments, below).
            )

            <h3 style="margin-bottom: 10px;">@item.Name</h3>

            <div class="">
                <div class="">
                    <img src="@item.ImgUrl" alt="" style="visibility: visible; opacity: 1;">
                </div>
            </div>

            <section>
                <p>@item.Description</p>
                <a href="#" class="btn btn-1">Read More</a>
                <p>@item.IsAccordion</p>
            </section>
        </div>

    </li>
}

There seems a problem with this line however:

@{
    Html.RenderPartial("_editPhoto" , Model);
}

Model gets underlined explaining that the Model passed into it (Photo) is not the right one..It seems that _editPhoto inherits the same Model as its parent maybe?

I managed to do this in the view:

@{
    var i = new Photography();
    Html.RenderPartial("_editPhoto", i);
}

The problem now is that the partialView gets rendered in a new window and not in its parent as I want it to.

UPDATE Im gonna give this one last go, wont create a new question:

This is my controller passing a photo to a partial view:

 public ActionResult EditPhoto(string id)
        {
            var photo = RavenSession.Load<ContentPage>(id) as Photography;

            return PartialView("_editPhoto", photo);               
        }

My "mainview" contains this code to render the partial view with the photo getting sent to it:

<div class="form-control">
          @{
            var model = new Photography();
            Html.Partial("_editPhoto",model);
                        }

           </div>

This opens up a new window where my new photo shows up. I would like it to get rendered inside of its parents view the same way as it gets rendered automaticaly when i first visit the page...

Upvotes: 1

Views: 16374

Answers (2)

Saranga
Saranga

Reputation: 3228

This questions seems related to one of your previous question.

Controller should be as follows;

public ActionResult EditPhoto(string id)
{
    var photo = RavenSession.Load<ContentPage>(id);
    return PartialView("_editPhoto", photo);
}

In your partial view _editPhoto, you can have the following code. I assume photo variable is a Photography object.

@model aPhoto_web.Models.AdminPages.Photography

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Photography</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
}

Thanks!

Upvotes: 3

Hiren Kagrana
Hiren Kagrana

Reputation: 932

Your controller action method should be:

    public ActionResult EditPhoto(string id)
    {
        var photo = RavenSession.Load<ContentPage>(id);
        return View("EditPhoto",photo);
    }

and your "EditPhoto" view should be:

    @{ Html.RenderPartial("_editPhoto",Model); }

Update

Your controller action method should be:

    public ActionResult Photos()
    {
        return View();
    }

    public ActionResult EditPhoto(string id)
    {
        var photo = RavenSession.Load<ContentPage>(id);
        return View(photo);
    }

your "EditPhoto" should be view (not a partialview), on link click, "EditPhoto" action method is called and it returns the "EditPhoto" view

Upvotes: 6

Related Questions