Why can't I parse the html result returned by my MVC partial view?

Rachael

I get that the purpose of returning MVC partial views as html was meant to just magically load everything in the viewmodel being passed into my razor-templated view.

However, I only want to selectively load that html model data into my view from my ajax call. I have found zero documentation on parsing this stuff, with the exception being a snippet which supposedly parses the unformatted html into something queryable using jquery:

 var modelHTML = $.parseHTML(viewModelData);

I want to go a step further and do:

 var elements = $('<div></div>');
 elements.html(modelHTML);
 var matchingOption = $(elements).find("#selectListID option:first");
 alert(matchingOption.val);

but this returns a bunch of jibberish. Is there any way to parse the html returned from a partial view result carrying a viewmodel, rather than returning a json result?

Here's the gist of my controller code:

  public ActionResult _MyPartialView(_PartialViewModel pvm)
    {
         _PartialViewModel _pvm = pvm;

         _pvm.shapesList = new List<SelectListItem>();

          foreach(var item in context.shapesEntity.Where(x => x.shapeName.HasValue))
          {
               _pvm.ShapesList.Add(new SelectListItem { Value = item.id, Text = item.name});
          }

          return PartialView("_myPartialView", _pvm);
     }

Note:: I have successfully replaced the HTML in my partial view with the html in this div. So that works and is the way I am seeing people use partial view html results. But I want to query this with jquery and use pieces of the result as I please.

Shriike

Try this out, I think I see where the problem is.

First of all on this line.

var modelHTML = $.parseHTML(viewModelData);

You don't need to use parseHTML (I doubt it'd hurt), because the return value of your action is already the HTML and you can just use that when setting the HTML of your div.

var elements = $('<div></div>');
elements.html(modelHTML);
var matchingOption = $(elements).find("#selectListID option:first");

All of that above looks fine, should do exactly what you want (assuming the HTML in your action has an ID called selectListID and it has an option in it).

I think the below line is where you have a mistake.

alert(matchingOption.val);

val is a function, so you're getting the function definition returned. What you want is something like this.

console.log(matchingOption.val());

You could of course use alert instead, I prefer console.log, you could also use console.log(matchingOption) to just spit out the object to your console.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why can't I parse YAML with Ruby?

分類Dev

Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

分類Dev

Can't Update Partial View With Ajax ASP.NET Core MVC

分類Dev

I can't link my JQuery file to my HTML file

分類Dev

backbone view why can't I trigger UI event in initialize?

分類Dev

Why can't I select cells in my WPF datagrid?

分類Dev

Why can't I use my column alias in WHERE clause?

分類Dev

Why can't I extend my desktop 12.04 in KDE?

分類Dev

Why can't I crash my system with a fork bomb?

分類Dev

Why can't I see remote video in my WebRTC app?

分類Dev

Why can't I open my txt file in Ubuntu?

分類Dev

Why can't I download my package through packagist?

分類Dev

Why can't I access my objects member variable?

分類Dev

Why my custom js in bundle doesn't show up in custom view MVC5

分類Dev

I can't use a JS script in my HTML with Vue js

分類Dev

How can I refer to a property of a model within a partial view?

分類Dev

How can I get my hyperlink in HTML result in same page or window with or without using iframe tag or JavaScript?

分類Dev

Why I cannot parse this HTML page in Python?

分類Dev

Why isn't my HTML file showing up in MVC 4 when it's in the Shared folder?

分類Dev

How can I pass HTML code stored in a string variable to a view in MVC 5?

分類Dev

What is the difference between view & partial view in mvc

分類Dev

Why I can't assign result of std::from_chars to std::tie?

分類Dev

Swift/XCode - Why can't I pass data to the next view controller when a tableViewCell is pressed?

分類Dev

Why doesn't my async function return any result?

分類Dev

Why isn't my html code outputting the variable's value when I use Flask to render a template?

分類Dev

Why can't I copy a large number of files to my USB flash drive?

分類Dev

Why can't I match the last part of my regular expression in python?

分類Dev

Why can't I add static files in my django project when other static files are available in sources?

分類Dev

Why can't I define my variable with bracket notation in Angular 2

Related 関連記事

  1. 1

    Why can't I parse YAML with Ruby?

  2. 2

    Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

  3. 3

    Can't Update Partial View With Ajax ASP.NET Core MVC

  4. 4

    I can't link my JQuery file to my HTML file

  5. 5

    backbone view why can't I trigger UI event in initialize?

  6. 6

    Why can't I select cells in my WPF datagrid?

  7. 7

    Why can't I use my column alias in WHERE clause?

  8. 8

    Why can't I extend my desktop 12.04 in KDE?

  9. 9

    Why can't I crash my system with a fork bomb?

  10. 10

    Why can't I see remote video in my WebRTC app?

  11. 11

    Why can't I open my txt file in Ubuntu?

  12. 12

    Why can't I download my package through packagist?

  13. 13

    Why can't I access my objects member variable?

  14. 14

    Why my custom js in bundle doesn't show up in custom view MVC5

  15. 15

    I can't use a JS script in my HTML with Vue js

  16. 16

    How can I refer to a property of a model within a partial view?

  17. 17

    How can I get my hyperlink in HTML result in same page or window with or without using iframe tag or JavaScript?

  18. 18

    Why I cannot parse this HTML page in Python?

  19. 19

    Why isn't my HTML file showing up in MVC 4 when it's in the Shared folder?

  20. 20

    How can I pass HTML code stored in a string variable to a view in MVC 5?

  21. 21

    What is the difference between view & partial view in mvc

  22. 22

    Why I can't assign result of std::from_chars to std::tie?

  23. 23

    Swift/XCode - Why can't I pass data to the next view controller when a tableViewCell is pressed?

  24. 24

    Why doesn't my async function return any result?

  25. 25

    Why isn't my html code outputting the variable's value when I use Flask to render a template?

  26. 26

    Why can't I copy a large number of files to my USB flash drive?

  27. 27

    Why can't I match the last part of my regular expression in python?

  28. 28

    Why can't I add static files in my django project when other static files are available in sources?

  29. 29

    Why can't I define my variable with bracket notation in Angular 2

ホットタグ

アーカイブ