来自另一个控制器

时间:2015-10-21 13:11:03

标签: c# asp.net-mvc linq razor

尝试从部分视图中获取TempData条消息时出现问题, 我正在主视图中渲染另一个控制器的局部视图,如下所示:

@{
    UserProfile Model = ViewData["model"] as UserProfile;
}

@{Html.RenderPartial("~/Views/MyProfile/Index.cshtml", Model);
}

但是我似乎无法在主视图中甚至在PartialView中访问此局部视图的TempData消息?提交更改后没有显示任何内容。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Update(UserProfile model, HttpPostedFileBase Picture)
    {
        if (ModelState.IsValid)
        {
            if (Picture != null && Picture.ContentLength > 0)
            {
                // delete first
                string todelete = model.PreviousPicture;
                if (todelete != null && !todelete.Equals(""))
                {
                    //....
                }

                //....

                UserProfile up = _db.UserProfiles.SingleOrDefault(u => u.UserId == model.UserId);
                //....

                try
                {
                    UpdateModel(up);
                }
                catch (Exception)
                {
                    TempData["Message"] = "Please ensure all required fields are supplied.";
                    return RedirectToAction("Index");
                }

                if (ModelState.IsValid)
                {
                    //...
                    _db.SaveChanges();
                    TempData["Success"] = "Profile was updated successfully.";
                    return RedirectToAction("Index");
                }

            }
            else
            {

                UserProfile up = _db.UserProfiles.SingleOrDefault(u => u.UserId == model.UserId);

                //.....
                try
                {
                    UpdateModel(up);
                }
                catch (Exception)
                {
                    TempData["Message"] = "Please ensure all required fields are supplied.";
                    return RedirectToAction("Index");
                }

                if (ModelState.IsValid)
                {
                    _db.Entry(up).CurrentValues.SetValues(model);
                    _db.SaveChanges();
                    TempData["Success"] = "Profile was updated successfully.";
                    return RedirectToAction("Index");
                }
            }
        }

        TempData["Message"] = "Please ensure all required fields are supplied.";

        return RedirectToAction("Index");
    }
}

在局部视图中,我显示如下消息:

@Html.Partial("_ValidationPartial")

但是,在我提交后重定向后,部分中没有显示消息?

这是_validationPartial:

@if (!Html.ViewData.ModelState.IsValid)
{
    foreach (ModelState modelState in ViewData.ModelState.Values)
    {
        foreach (ModelError error in modelState.Errors)
        {
            <div class="alert alert-error alert-warning alert-dismissable message-error"
                 style="font-size: 13px; color: #d01616; margin-bottom: 0px; padding: 10px;
       text-align:left;padding-left:30px;">
                @error.ErrorMessage
            </div>
        }
    }
    <br />
}

@if (@TempData["Message"] != null)
{
    <div class="alert alert-error alert-warning alert-dismissable message-error"
         style="font-size: 13px; color: #d01616; margin-bottom: 0px; padding: 10px;
       text-align:left;padding-left:30px;">
        @TempData["Message"]
    </div>
    <br />
}
@if (@TempData["Success"] != null)
{
    <div class="alert alert-success message-success"
         style="font-size: 13px; color: green; margin-bottom: 0px; padding: 10px;
       text-align:left;padding-left:30px;">
        @TempData["Success"]
    </div>
    <br />
}

1 个答案:

答案 0 :(得分:0)

RedirectToAction()向浏览器返回HTTP 302响应,这会导致浏览器向指定的操作发出GET请求。它使ViewState成为可能 错误消失。

返回View(model)代替RedirectToAction()可能会有所帮助。