String parameter in AjaxOption null to send but show in response

In this code snippet

@using (Ajax.BeginForm("MyAction", "MyRouteValues", new AjaxOptions { OnSuccess = "myJSFunction(" + Model.IntegerParameter + ", '" + Model.StringParameter + "')" })) 

Why is my Javascript correctly recognizing Model.IntegerParameter, but Model.StringParameter as null ? I am sure that he has data about it when I check the answer, and he shows it as

 data-ajax-success="myJSFunction(111111, 'AAAAAA')" 

My View model is really simple and looks like this:

 public class MyViewModel { public int IntegerParameter { get; set; } public string StringParameter { get; set; } } 

How to fix it?

Information added

I tried to change the second parameter to int, now it is not passed as null , but 0 and still it shows in the answer in FireBug.

I added Html.Raw, but it still gets null in Javascript.

Here is a screenshot of the real world about what I get in response to the console:

enter image description here

--------------- Another update ------------------

I tried all the sentences, but it seems like this is a BUG in MVC s # arp ? I tried in different projects, and on different PCs it is all the same for me. I noticed that this only happens if it comes from the model, it looks like it happens between the Javascript response, the value of the line is lost regardless of whether it is the first, second or any position in the parameter, but if I use solid coded value, for example:

 myJSFunction(" + Model.IntegerParameter + ", 'AAAAAAAA')" 

I get a successful result, also if I use jQuery, for example:

 myJSFunction(" + Model.IntegerParameter + ", $('#SearchString').val())" 

This also works, but if I pass in a model which is a string like

 myJSFunction(" + Model.IntegerParameter + ", '" + Model.StringParameter + "')" 

This does not work.

So, you want to see what really happens in the real world, where I took into account the proposals of @Darin and @Shark. Here is a screenshot:

enter image description here

As you can see in the answer, it is there, but when it is passed to Javascript, it is lost. Here is the real Javascript as well

 displayResultsPopUpWindow: function (model) { var postData = { transactionId: model.transactionId, searchString: model.searchString }; $.post("/Invoicing/GetSearchResults", postData, function (data) { WindowHelper.displayWindow("Add Airline Transaction", "<div id='matchBookingResults'>" + unescape(data.viewHtml) + "</div>", 640, 500); }); }, 
+6
source share
4 answers

Looks like a bug in the S # arp architecture, so instead of using the model, I just used jQuery to retrieve the value I need

 myJSFunction(" + Model.IntegerParameter + ", $('#SearchString').val())" 
0
source

Unable to reproduce the problem. Try it like this:

Model:

 public class MyViewModel { public int IntegerParameter { get; set; } public string StringParameter { get; set; } } 

Controller:

 public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel { IntegerParameter = 1111, StringParameter = "AA'AA\"AA" }); } [HttpPost] public ActionResult MyAction() { return Json(new { foo = "bar" }); } } 

View:

 @model MyViewModel <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> <script type="text/javascript"> var myJSFunction = function (model) { alert('intParam=' + model.intParam + ', strParam=' + model.strParam); }; </script> @using (Ajax.BeginForm( "MyAction", "Home", new AjaxOptions { OnSuccess = "myJSFunction(" + Json.Encode(new { intParam = Model.IntegerParameter, strParam = Model.StringParameter }) + ")" } )) { <button type="submit">OK</button> } 

When a form is submitted using AJAX, the success callback of myJSFunction is called and passes the correct values.

+4
source

It looks like this is a string encoded in HTML. Try to do this:

 Html.Raw(Model.StringParameter) 

Change Try the following:

 OnSuccess = Html.Raw("myJSFunction(" + Model.IntegerParameter + ", '" + Model.StringParameter + "')") 
+1
source

Your searchString is null because the input is named SearchString - the register has facilities.

0
source

Source: https://habr.com/ru/post/909594/


All Articles