简体   繁体   English

返回PartialView时在脚本中使用TempData

[英]Using TempData inside scripts when returning a PartialView

I am trying to pass TempData value to my view, but my controller action is an ajax post controller and returns a partialview. 我正在尝试将TempData值传递给我的视图,但是我的控制器动作是ajax发布控制器,并返回了partialview。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CategoryViewModel model)
{
    if (ModelState.IsValid)
    {
        var category = new Category()
        {
            Name = model.Name,
            Description = model.Description
        };
        //test
        System.Threading.Thread.Sleep(2000);

        try
        {
            _repository.AddCategory(category);

            TempData["success"] = string.Format("Category Added");
            return PartialView("~/Areas/Dashboard/Views/Category/_List.cshtml", CategoryListMap());
        }
        catch(Exception ex)
        {
            TempData["error"] = string.Format("{0}", ex);
            return PartialView("~/Areas/Dashboard/Views/Category/_List.cshtml", CategoryListMap());
        }
    }

    TempData["error"] = string.Format("Modal state is not valid");
    return PartialView("~/Areas/Dashboard/Views/Category/_List.cshtml", CategoryListMap());
}

My partial view of a create form: 我的创建表单局部视图:

@using (Ajax.BeginForm("Create", "Category", new { area = "dashboard" }, new AjaxOptions { UpdateTargetId = "categories", OnComplete = "onComplete", OnBegin = "onBegin" }))
{
    @Html.AntiForgeryToken()
    <div class="modal-body">
        <div class="form-group">
            @Html.LabelFor(m => m.Name)
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Name)
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Description)
            @Html.TextAreaFor(m => m.Description, new { @class = "form-control", rows = "5" })
            @Html.ValidationMessageFor(m => m.Description)
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
        <button class="ladda-button btn btn-primary" type="submit" data-style="zoom-in">Add</button>
    </div>
}

So my controller must returns a partialview because of the ajax form, and the value TempData is passed to the _list partialview . 因此,由于ajax形式,我的控制器必须返回一个partialview,并且将TempData值传递给_listpartialview

@if (TempData["error"] != null)
{
    <p>
        <div class="alert alert-danger alert-dismissable">
            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
            @TempData["error"]
        </div>
    </p>
}
@if (TempData["success"] != null)
{
    <p>
        <div class="alert alert-success alert-dismissable">
            <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
            @TempData["success"]
        </div>
    </p>
}

<table class="table table-striped table-bordered table-hover dataTables">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var category in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(m => category.CategoryId)
                </td>
                <td>
                    @Html.DisplayFor(m => category.Name)
                </td>
                <td>
                    @Html.DisplayFor(m => category.Description)
                </td>
            </tr>
        }
    </tbody>
    <tfoot>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </tfoot>
</table>

As you can see I'm getting the TempData value inside my partialview, everything is working fine but the problem I'm having is how do I get the TempData value inisde my view instead? 如您所见,我在我的partialview中获取了TempData值,一切工作正常,但是我遇到的问题是如何在我的视图中获取TempData值? I tried to get the TempData value inside the view itself but its not working because I think in my controller I'm returning a partialview and the TempData value will only display when a partialview is been requested? 我试图在视图本身内部获取TempData值,但它不起作用,因为我认为在控制器中我正在返回一个partialview,而TempData值仅在请求partialview时才会显示?

Here is my full view: 这是我的全貌:

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Categories</h2>
        <ol class="breadcrumb">
            <li>
                <a href="@Url.Action("Index", "Category")">Categories</a>
            </li>
            <li class="active">
                <strong>List</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">
    </div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal1">
                        <i class="fa fa-plus"></i>
                    </button>
                </div>

                <div class="ibox-content table-responsive">
                    <div id="categories">
                        @{Html.RenderAction("List", "Category", new { area = "dashboard" });}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal inmodal" id="myModal1" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content animated fadeIn">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">Add a category</h4>
            </div>
            @{ Html.RenderAction("Create", "Category", new { area = "dashboard" });}
        </div>
    </div>
</div>

@section Scripts {
    @Scripts.Render...blahblah

    <script type="text/javascript">

        function onSuccess() {
            //remove toastr
            toastr.remove();
            **//not working, tempdata value not set**
            toastr.success('@TempData["success"]', 'Success');
        }

        function onFailure() {
            //remove toastr
            toastr.remove();
            **//not working, tempdata value not set**
            toastr.error('@TempData["error"]', 'Error');
        }
    </script>
}

If I try to use TempData value inside the full view, no value is set...I can't render Scripts inside a partialview and from my understanding you shouldn't write any scripts inside a partialview as well, so how can I use the tempdata value inside the script when I'm returning a partial view with tempdata??? 如果我尝试在完整视图中使用TempData值,则未设置任何值...我无法在PartialView中呈现脚本,并且据我了解,您也不应在PartialView中编写任何脚本, 所以我该如何使用返回带有tempdata的部分视图时脚本中的tempdata值???

You use of TempData is not appropriate (which is for passing data between controller methods), and for passing data from a controller to a view, you should be using ViewBag or ViewData (or better still, a property in a view model). 您不适合使用TempData (这是用于在控制器方法之间传递数据),并且对于将数据从控制器传递到视图,您应该使用ViewBagViewData (或者更好的是,在视图模型中使用属性)。

But the issue is that your making an ajax call which receives the html of the partial view your controller creates. 但是问题是您进行了ajax调用,以接收控制器创建的部分视图的html。 Your not sending the value of TempData["error"] or TempData["success"] back to the client, just the html of your partial view. 您不会将TempData["error"]TempData["success"]的值发送回客户端,而只是将部分视图的html发送回客户端。

You could do this by adding your message to a ViewBag property 您可以通过将消息添加到ViewBag属性来完成此操作

ViewBag.Message = "...."; // your success or error message

and in the partial view, create a hidden input to store the value 在局部视图中,创建一个隐藏的输入来存储值

<input type="hidden" id="message" value="@ViewBag.Message" />

and the in the ajax success calback, you can access it using 并且在ajax成功回溯中,您可以使用

success: function(data) {
    $('someElement').append(data); // append the partial view to the DOM
    var message = $('#message').val(); // get the value of the message
    toastr.success(message , 'Success'); // display it
}

However, returning a partial of the whole table and replacing it in the view is not very efficient and you should consider making your ajax call to save the data and then returning json to indicate success or otherwise and then just appending a new table ros based on the values in your form. 但是,返回整个表的一部分并在视图中替换它不是很有效,您应该考虑进行ajax调用以保存数据,然后返回json表示成功或其他,然后仅基于添加一个新表ros表单中的值。 Refer this DotNetFiddle for an example. 请参考此DotNetFiddle示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM