0

I have a view which needs to display a table of data. Lets just say people.

So in my web api app, I create the view that could use razor to allow me to display these people:

@foreach(Person person in Model.People)
{
  <tr>
     <td>@person.FirstName</td>
     <td>@person.LastName</td>
  </tr>
}

I'm not even using the ApiController class yet.

So then lets say the user enters in some criteria in a search and the Model.People list needs to update. I would then use my controller (I think) to update my Model.People list. This is the functionality I need.

BUT I could use ALSO jQuery to hit my restful ApiController class, and dynamically update my people table something like this (although I don't think I have the syntax right):

$.getJSON('api/people/updatepeople', function(data) {
    var fileTable = $("#personTable");
    $.each(data, function() {
       var row = $('$(this).Name$(this).FirstName', '$(this).Name$(this).LastName');
       $("#personTable").append( row );
    });
});

Syntax aside, what should I be doing? Razor or jQuery? Is it better or more efficient to use razor with models and a Controller, with no ApiController? Or use jQuery with an ApiController? It seems these technologies go hand in hand, razor with a controller, jQuery with a ApiController. I feel like I shouldn't be mixing them, like using jQuery with razor and models...?

Would appreciate any insights into what is the more correct approach (if there is one) and why.

4
  • Amazing! Mere seconds after posting I get a close vote. Why? Is SO no longer a place to learn? Ask questions? This is a very specific question in regards to best practice when working with web.api. The question provides the scenario with examples. I don't see this igniting a fierce debate of religious proportions, so a close vote of 'Not Constructive' seems a bit harsh. Commented Mar 13, 2013 at 4:32
  • Nicros, the question is admittedly fairly broad, so there's no one "right" answer. I'm not the one who voted to close it, but this is almost more of a wiki question and not a "why doesn't my code work the way I expected" question. Commented Mar 13, 2013 at 4:42
  • @Eilon That may be but how would one know there is no one 'right' answer without asking the question? I'm no expert on web api, it may be that someone with more experience DOES have a right answer. Commented Mar 13, 2013 at 4:48
  • Nicros - I totally agree. Many times I have seen people hit "Close" before even asking a clarifying question. I find a better approach is for people to ask clarifying questions, and only if the asker is unresponsive or otherwise unhelpful, it's time to close. Commented Mar 13, 2013 at 6:13

1 Answer 1

1

This entirely depends on you scenario and what you want to accomplish. If you want partial page updates with no post backs then go ahead and use asynchronous XmlHttpRequests to update your page. Otherwise you can display your loaded model on the page between page post backs.

Mixing the technologies is fine, thats the approach im taking for my current project. For example i load the initial page and display the model as required, then for small updates on the page i perform Javascript GETs/POSTs.

By the way your can generate asynchronous forms / action links with razor, so its not really a questions of Razor or JQuery, its a comparison of asynchronous updates version synchronous postbacks.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Initially my page may show nothing, waiting for user input to perform the search. It sounds like using jQuery in this case is fine... it's what I'm doing actually, it just bothered me that I'm creating a view with a controller and do use models, but only through jQuery and getting JSON back for my models. So I'm not actually not 'using' razor.
Thats fine then if its the way you want to go. You can either post back to the current actionresult with the search criteria and then show the new updated model with the search results, or do like you currently are. Both ways are relevant.

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.