无法使用TempData或jquery.cookie显示消息

时间:2015-06-17 19:33:59

标签: jquery asp.net-mvc asp.net-ajax jquery-cookie tempdata

我使用Ajax调用Action并尝试显示从ControllerView的已返回消息。但是,虽然该消息随TempData一起返回,但它不显示在视图上(实际上我在@Html.BeginForm()中显示它,但这次我使用<form>并且可能是由于那)。另一方面,我尝试按照this页面上的说明使用 jquery.cookie ,但无法显示该消息。您能否通过在TempDatajquery.cookie上显示必要的行,为ControllerView提供示例?提前谢谢。

查看:

<form id="frmCreate"  action="Create" method="post" enctype="multipart/form-data" >

@if (TempData["message"] != null)
{
    <div class="text-danger">@TempData["message"].ToString()</div>
}
//... removed for brevity
</form>


控制器:

public ActionResult Create([Bind(Exclude = null)] PersonViewModel person)
{
    //Some stuff (removed for brevity)
    TempData["message"] = "Record has been added...";
    return PartialView("Create", person);
}


===============================更新=============== ================

查看:

@model PersonViewModel

<form id="frmCreate"  action="Create" method="post" enctype="multipart/form-data" >

@Html.AntiForgeryToken()

<div class="container">

    @Html.ValidationSummary(true)

    <div><b>Message:@ViewBag.Message</b></div>

    @Html.LabelFor(m => m.ProjectID)
    @(Html.Kendo().DropDownList()
          .Name("ProjectID")
          //... removed for brevity
    )
    <br />

    @*Render Partialview according to Dropdownlists selectedIndex*@
    <!-- Place where you will insert your partial -->
    <div id="partialPlaceHolder" style="display:none;"></div>

</div>


<div class="modal-footer">
    @(Html.Kendo().Button().Name("btnCancel"))

    @(Html.Kendo().Button().Name("btnSubmit"))
        .HtmlAttributes(new { type = "submit"})
</div>

</form>



<script type="text/javascript">

//OnLoad method & functions::::::::::
$(function () {      
    var selectedProjectId = $('#ProjectID').val(); /* Get the selected value of dropdownlist */       
    var json = JSON.stringify(@Model);
    $.ajax({
        type: "POST",
        url: '@Url.Action("RenderPartialView", "Person")' + selectedProjectId,
        contentType: "application/json; charset=utf-8",
        data: json,
        dataType: "json",
        success: function (data) {
            // get the result and do some magic with it
            var message = data.Message;
            $("#resultMessage").html(message);
        }
    });


});


//For keeping model values we use <form> instead of Html.BeginForm()
$('form').submit(function (event) {
    event.preventDefault();
    $.post('Person/Create', $(this).serialize()).done(function () {

    }).fail(function () {

    });
});

//Render Partialview according to Dropdownlist's selectedIndex
$('#ProjectID').change(function () {  
    var selectedProjectId = $(this).val();
    $.get('/Person/RenderPartialView/' + selectedProjectId, function (data) {
        $('#partialPlaceHolder').html(data);
    });        
});

</script>

0 个答案:

没有答案