0

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...

user2915962
  • 2,691
  • 8
  • 33
  • 60
  • I'm not sure what you want here. Does the EditPhoto action contain the partial view? Basically you just need to populate **a** model. But your structure is unclear from the question. – Liam Jul 07 '14 at 09:24
  • Can you please provide requirement in more detail? What is your page structure and how you are calling the editPhoto method? is it a on load request or Ajax request? – K D Jul 07 '14 at 09:30

2 Answers2

5

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

Hiren Kagrana
  • 912
  • 8
  • 21
  • `RenderPartial` needs to be in `{}` braces [see here](http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction) – Liam Jul 07 '14 at 09:26
  • Thank you! That helped me a bit on the way, I will update my question to clearify.. – user2915962 Jul 07 '14 at 09:31
  • Thank you for answering, I cant see where my Photo is getting sent to the partial? Im sure you are right but I do not understand. – user2915962 Jul 07 '14 at 09:58
  • 1
    @user2915962 the flow of you application is: user access the URL `localhost/home/photos` which called the "Photos" action method of the home controller and return the view which displays the list of photos with link on the name, while user clicks on the photo name link, the "EditPhoto" action mehod called with the relavent photoId, the "EditPhoto" action method search for the elavent photo object and pass a new "EditPhoto" view whth the Photo object, on viewm you can use Photo object Model properties to built the edit form for photo details, thanks. Ask if further help needed!! – Hiren Kagrana Jul 07 '14 at 10:06
  • Are you saying that I should create a new view called EditPhoto that is not a partial view? – user2915962 Jul 07 '14 at 10:26
  • Kindly let me know if still problem persist, or else mark as answered – Hiren Kagrana Jul 07 '14 at 10:42
3

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!

Community
  • 1
  • 1
Saranga
  • 3,178
  • 1
  • 18
  • 26
  • Yes im still trying to get it to work...Could not get the JqueryUI to work...However, the above solution still opens a a new view instead of rendering in its parent. Thanks you. – user2915962 Jul 07 '14 at 09:54
  • Check my updated answer, controller method should return a PartialView. Thanks! – Saranga Jul 07 '14 at 10:01
  • Im sorry but it still open up a new window even when i used your updated answer. Im going crazy here =) Please look at bottom of my question if I have done correctly in the parent view if you have the time.. – user2915962 Jul 07 '14 at 10:12