0

In my Project i need to pass value partial value while click button. So i tried like this

<div id="searchgrid" class="col-lg-12">
  @Html.Partial("ResultGrid", new List<SCM_MVC.Models.User>(), 
  new ViewDataDictionary(this.ViewData) { { "index" , xEdit } })
</div>

<script src="~/assets/js/jquery-1.10.2.js"></script>
<script>
    var url = '@Url.Action("ResultGrid", "User")', new { xEdit : ViewData["Form"] };
    $('#btnloadgrid').click(function () {
        $('#searchgrid').load(url);
    })
</script>

While open View its Working. But Click button its not working. What am doing wrong in jquery function?

3
  • var url = '@Url.Action("ResultGrid", "User")', new { xEdit : ViewData["Form"] }; will create an invalid output in the JS. If you check the actual rendered code (and the console) you'll see the syntax issues. Commented Jul 9, 2018 at 7:06
  • 1
    What is ViewData["Form"]? That would only work if its a simple value (and the new { .. } would need to be withing the Url.Action()) Commented Jul 9, 2018 at 7:06
  • @StephenMuecke thanks Commented Jul 9, 2018 at 10:27

1 Answer 1

1

The url declaration is syntactically wrong as checked in .NET fiddle:

var url = '@Url.Action("ResultGrid", "User")', new { xEdit : ViewData["Form"] };
//                                             ^ Syntax error: Unexpected token 'new'

The correct way is either wrap route value parameters inside UrlHelper.Action helper:

var url = '@Url.Action("ResultGrid", "User", new { xEdit = ViewData["Form"] })';

Or by using old-fashioned query string if the value passed from client-side variable:

var viewData = '@ViewData["Form"]';

var url = '@Url.Action("ResultGrid", "User")?xEdit=' + viewData;

Both ways produce URL as in example below (ViewData["Form"] must be such like plain numeric or string value):

/User/ResultGrid?xEdit=someValue
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.