mvc4

2013-03-25 6 views
5

में जावास्क्रिप्ट में tempdata एक्सेस करें मैं जावास्क्रिप्ट में TempData तक पहुंचने का प्रयास कर रहा हूं। लेकिन शून्य मूल्य प्राप्त हो रहा है। मैं रिकॉर्ड अपडेट करने के लिए AJAX कॉल कर रहा हूं और मैं रिकार्ड अद्यतन सफलतापूर्वक संदेश प्रदर्शित करना चाहता हूं। जो नियंत्रक से UpdateOperation कार्रवाई से आएगा। लेकिन वर्तमान में यह शून्य मान प्रदर्शित करेगा। मैं भी Firebug के साथ जांच इस प्रकार के रूप में यह पता चलता है:mvc4

function onComplete(e) { 

if (e.name == "update") { 

alert(''); 

} 

यहाँ नियंत्रक

public class OperationController : BaseController 
    { 
     /// <summary> 
     /// Index action will return template view of the page without data 
     /// </summary> 
     /// <returns>Blank Action</returns> 
     public ActionResult Index() 
     { 
      return this.View(); 
     } 

     /// <summary> 
     /// Get all Operation from System 
     /// </summary> 
     /// <returns>return action result</returns> 
     [GridAction] 
     public ActionResult SelectOperation() 
     { 
      IEnumerable<OperationEntity> operationList = OperationComponent.GetAll(); 
      return this.View(new GridModel(operationList)); 
     } 

     /// <summary> 
     /// Method for update operation 
     /// </summary> 
     /// <param name="entity">moduleViewModel to update Module</param> 
     /// <returns>return action result</returns> 
     [GridAction] 
     public ActionResult UpdateOperation(OperationEntity entity) 
     { 
      if (ModelState.IsValid) 
      { 
       entity.Log = new BusinessCore.BusinessEntities.LogDetails(); 
       entity.Log.ModifiedBy = SessionHelper.UserId; 
       Status status = OperationComponent.Update(entity); 
       this.TempData["AlertMessage"] = status.Message; 
       this.ViewData["_AlertMessage"] = status.Message; 
       return this.View(new GridModel(OperationComponent.GetAll())); 
      } 
      else 
      { 
       return this.View(entity); 
      } 
     } 
    } 

की मेरी कोड है मेरे विचार

@using Telerik.Web.Mvc.UI; 
@{ 
    ViewBag.Title = "Operation List"; 
} 

<h2>@ViewBag.Title</h2> 
<script src="../../../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
// function onSave(e) { 
//  alert('Record Save Succesfully'); 
// } 
    function onComplete(e) { 
     if (e.name == "update") { 
      alert('@TempData["AlertMessage"]'); 
      alert('@ViewData["_AlertMessage"]'); 
     } 
     if (e.name == "insert") { 
      alert("Operation Inserted Successfully"); 
     } 
     if (e.name == "delete") { 
      alert("Operation Deleted Successfully"); 
     } 
    } 
    function newAlert(type, message) { 
    if (message != "" || message != null) { 
     $("#alert-area").append($("<div class='alert alert-success " + type + " fade in' data-alert><p><b> " + message + " </b></p></div>")); 
     $(".alert-success").delay(4000).fadeOut("slow", function() { $(this).remove(); }); 
    } 
} 
</script> 

@(Html.Telerik().Grid<QuexstERP.BusinessCore.BusinessEntities.SysAdmin.OperationEntity>() 
     .Name("Grid") 
     .DataKeys(keys => 
     { 
      keys.Add(p => p.Id); 
     }) 
        .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Image).ImageHtmlAttributes(new { style = "margin-left:0", title = "Add" })) 
     .DataBinding(dataBinding => 
     { 
      dataBinding.Ajax() 
       .Select("SelectOperation", "Operation") 
       .Insert("InsertOperation", "Operation") 
       .Update("UpdateOperation", "Operation") 
       .Delete("DeleteOperation", "Operation"); 
     }) 
     .Columns(columns => 
     { 
      columns.Command(commands => 
      { 
       commands.Edit().ButtonType(GridButtonType.Image).HtmlAttributes(new { title = "Edit" }); 
       commands.Delete().ButtonType(GridButtonType.Image).HtmlAttributes(new { title = "Delete" }); 
      }).Width(80).Title("Commands"); 
      columns.Bound(p => p.Name).Width(200).Title("Operation Name"); 
      columns.Bound(p => p.Description).Width(310).Title("Description"); 
     }) 
     .ClientEvents(events => events 
       .OnComplete("onComplete") 
       ) 
        .Editable(editing => editing.Mode(GridEditMode.PopUp).InsertRowPosition(GridInsertRowPosition.Top)) 

     .Pageable() 
     .Scrollable() 
     .Sortable() 
     .Filterable()   
) 
@section HeadContent { 
<style type="text/css"> 
    .field-validation-error 
    { 
     position: absolute; 
     display: block; 
    } 

    * html .field-validation-error { position: relative; } 
    *+html .field-validation-error { position: relative; } 

    .field-validation-error span 
    { 
     position: relative; 
     white-space: nowrap; 
     color: red; 
     padding: 10px 5px 3px; 
     background: transparent url('@Url.Content("~/Content/Common/validation-error-message.png") ') no-repeat 0 0; 
    } 

    /* in-form editing */ 
    .t-edit-form-container 
    { 
     width: 480px; 
     margin: 1em; 
    } 

    .t-edit-form-container .editor-label, 
    .t-edit-form-container .editor-field 
    { 
     padding-bottom: 1em; 
     float: left; 
    } 

    .t-edit-form-container .editor-label 
    { 
     width: 25%; 
     text-align: right; 
     padding-right: 3%; 
     clear: left; 
    } 
    .t-edit-form-container .editor-field textarea 
    { 
    font-size:11px; 
    width:80%; 
} 
    .t-edit-form-container .editor-field 
    { 
     width: 70%; 
    } 
</style> 
} 
+0

अपने if स्टेटमेंट में 'डीबगर' रखने का प्रयास करें जो e.name == "अपडेट" हो जाता है और जांचें कि यह डीबगर के लिए आता है या नहीं –

+0

याप मैंने पहले ही ऐसा किया है। यह आ रहा है लेकिन सतर्क ('') ; मान शून्य है क्योंकि मैं सर्वर पर AJAX कॉल कर रहा हूं। यह टेम्पपडाटा में कैल्यू हो जाता है लेकिन यह जावास्क्रिप्ट –

उत्तर

13

मैं अपने एक पुराने सवाल पता, लेकिन मैं मैं का जवाब था सोचा के रूप में मैं देख रहा था एक ही समाधान के लिए और आशा है कि कुछ अन्य लोगों की मदद करें।

यह मैं how-to-get-the-tempdata-in-javascript

अनिवार्य रूप से, आपका वाक्य रचना याद आ रही है कोष्ठक

//Your code 
alert('@TempData["AlertMessage"]'); 

// Correct code 
alert('@(TempData["AlertMessage"])'); 

ब्रैकेट @

आशा इस मेरे जैसे अगले खोजकर्ता में मदद करता है के बाद के लिए इसे हल।

2

TempData जब इस्तेमाल किया कार्रवाई पर पुन: निर्देशित किया। ViewBag आज़माएं।

नियंत्रक में

:

ViewBag.AlertMessage = status.Message; 

ध्यान में रखते हुए:

@{ 
    ViewBag.Title = "Operation List"; 
    string alert = "Your custom error message"; 

    if(ViewBag.AlertMessage != null) 
    { 
     alert = (string)ViewBag.AlertMessage; 
    }   
} 

और जावास्क्रिप्ट

var jsAlert = '@alert'; 
alert(jsAlert); 
+0

का ताज़ा मूल्य नहीं है आपका उत्तर स्मार्ट लगता है, लेकिन मुझे एक त्रुटि मिलती है "वर्तमान संदर्भ में नाम 'अलर्ट' मौजूद नहीं है"। दूसरी ओर, Html.BeginForm के साथ मैं TempData या ViewBag का उपयोग कर नियंत्रक से देखने के लिए संदेश भेज सकता हूं। लेकिन अजाक्स के साथ, मैं पास नहीं कर सकता। तो, आपका जवाब इस समस्या को हल करता है? या कौन से परिवर्तन लागू किए जाने चाहिए? अग्रिम में धन्यवाद। –

+0

आपको यह त्रुटि मिलती है ("नाम 'अलर्ट' वर्तमान संदर्भ में मौजूद नहीं है"), क्योंकि 'अलर्ट' वैरिएबल जेएस कोड और शीर्ष दृश्य के साथ एक ही पृष्ठ में होना चाहिए। दूसरा, क्या आप एजेक्स के साथ नियंत्रक को देखने या देखने के लिए नियंत्रक से डेटा पास करना चाहते हैं? मुझे सवाल ठीक नहीं हो सकता है। –

+0

मैं नियंत्रक से देखने के लिए डेटा पास करना चाहता हूं। दूसरी तरफ, आपके उत्तर में आवश्यक चर टाइप करना बेहतर होगा ताकि अन्य लोग जो पर्याप्त अनुभव न करें, आसानी से उनका उपयोग कर सकें। धन्यवाद। –