2

I'm trying to write Update function via ajax. These are my controller methods:

    [ActionName("UpdatePhoto"), HttpPost]
    public ActionResult UpdatePhotoPostViewModel(PhotoViewModel photo)
    {
        if (!ModelState.IsValid)
            return PartialView("UpdatePhoto", photo);
        TempData["Photo"] = photo;
        return Json(new { success = true });
    }

    [ActionName("UpdatePhoto"), HttpGet]
    public ActionResult UpdatePhotoGetViewModel(PhotoViewModel photo)
    {
        if (TempData["Photo"] != null)
            photo = TempData["Photo"] as PhotoViewModel;
        return PartialView(photo);
    }
    public ActionResult Photo(PhotoViewModel photo)
    {
        if (TempData["Photo"] != null)
            photo = TempData["Photo"] as PhotoViewModel;
        return PartialView("PhotoSummary", photo);
    }
}

And View classes:

//PhotoSummary
@model PhotoAlbum.WEB.Models.PhotoViewModel
<div class="well">
    <h3>
        <strong>@Model.Name</strong>
        <span class="pull-right label label-primary">@Model.AverageRaiting.ToString("# stars")</span>
    </h3>
    <span class="lead">@Model.Description</span>
    @Html.DialogFormLink("Update", Url.Action("UpdatePhoto", new {photo = @Model}), "Update Photo", @Model.PhotoId.ToString(), Url.Action("Photo"))
</div>

//Main View
@model PhotoAlbum.WEB.Models.PhotoListViewModel
@{
    ViewBag.Title = "My Photos";
}
@foreach (var p in @Model.Photos)
{
    <div [email protected]()>
        @Html.Action("Photo", new {photo = p})
    </div>
}

Here my DialogFormLink helper:

public static MvcHtmlString DialogFormLink(this HtmlHelper htmlHelper, string linkText, string dialogContentUrl,
     string dialogTitle, string updateTargetId, string updateUrl)
{
    TagBuilder builder = new TagBuilder("a");
    builder.SetInnerText(linkText);
    builder.Attributes.Add("href", dialogContentUrl);
    builder.Attributes.Add("data-dialog-title", dialogTitle);
    builder.Attributes.Add("data-update-target-id", updateTargetId);
    builder.Attributes.Add("data-update-url", updateUrl);
    builder.AddCssClass("dialogLink");
    return new MvcHtmlString(builder.ToString());
}

The problem is that object photo in UpdatePhotoGetViewModel method is null. But I passed the model here:

@Html.DialogFormLink("Update", Url.Action("UpdatePhoto", **new {photo = @Model}**), "Update Photo", @Model.PhotoId.ToString(), Url.Action("Photo"))

Why this parameter wasn't injected?

12
  • What is DialogFormLink()? (its not part of MVC). Commented Dec 17, 2015 at 7:35
  • @StephenMuecke added DialogFormLink class. Commented Dec 17, 2015 at 7:44
  • Its unclear what your trying to do here. A get method should not have your model as a parameter. And what is the point of sending a model to the voew and then sending it all back again unchanged. You should be just sending an id property an getting the model again based on the id. Commented Dec 17, 2015 at 7:49
  • If you did want to do the then its new { Model } (not new { photo = @Model } but not only will it generate an ugly query string, it will fail if the model contains any properties which are complex objects or collections, and your could exceed the query string limit and throw an exception. Commented Dec 17, 2015 at 7:51
  • @StephenMuecke I just don't wanna make a new request to the server for getting an object Photo 'cause I have this object in main view. Is it possible to avoid this operation? I can change the model after I click "Update" (it calls an ajax dialog form). Commented Dec 17, 2015 at 8:24

1 Answer 1

1

In order to pass the model back to a GET method, your code would need to be

@Html.DialogFormLink("Update", Url.Action("UpdatePhoto", Model), "Update Photo", ....)

However, generally you should not be doing this for the following reasons

  1. It creates an ugly query string
  2. The query string that it generates could exceed the query string limit and throw and exception
  3. If you model contains any properties which are complex objects or collections, model binding will fail and those properties will be null

Instead, you should pass the ID of the model to the GET method, and get the model from your repository (database, Session etc), for example

@Html.DialogFormLink("Update", Url.Action("UpdatePhoto", new { id = Model.ID }), "Update Photo", ....)

and in the controller

public ActionResult UpdatePhoto(int ID)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.