将参数传递给Url.Action方法有问题

时间:2015-12-17 07:32:31

标签: c# ajax asp.net-mvc-5 http-post http-get

我正在尝试通过ajax编写Update函数。 这些是我的控制器方法:

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

和View类:

//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 id=@p.PhotoId.ToString()>
        @Html.Action("Photo", new {photo = p})
    </div>
}

这是我的DialogFormLink助手:

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

问题是UpdatePhotoGetViewModel方法中的对象照片是null。 但我在这里通过了模型:

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

为什么不注入此参数?

0 个答案:

没有答案