Asp.Net Razor with Json

Asked

Viewed 255 times

2

I have a question and I have researched several sites but I did not get the answer. I develop systems in .Net with C# in MVC standard and use the on-screen presentation in the traditional View with Razor.

It happens that I have noticed that several web systems made available lately have presented more and more a user experience like a Desktop application, that is, with almost no Reloads per request and a greater interactivity with the events of the fields.

Many of these systems use Knockout, Vue, Aurelia and mainly Angularx in their Views, or better, use only the MC and the V is on account of other technology.

Now comes my question, how do I not master any of these technologies (yet), with your experience, what would be the best way to work with Views in such a way that I could create the systems with a user experience closer to those provided by the technologies mentioned above maintaining the MVC development standard with Razor? By the power that Views generated with Razor offers, a lot probably I shouldn’t be using some resource that this tool can provide?! - So I could observe one of the things that "must" work to arrive at the expected result is to work with Json (so far so good) as if my application were a Web API - I’m sure?

I hope I was clear in presenting my doubt.

Thanks in advance for the strength!

  • 1

    If what you want is only that parts of your system are asynchronous, you can search for partial view, ajax Forms or even Gypsy suggestion...

  • Aline, thanks for the tips. When you say ajax Forms is the same as Ajax.Beginfor() ? if yes, you believe that this can solve in large parts my questions?

  • If it’s just parts of the system that you want to send data or manipulate into asynchronous form, it will work yes.

1 answer

2


From what I could observe one of the things that "must" work to get the expected result is to work with Json (so far so good) as if my application was a Web API - I’m sure?

Even better: working with the Web API itself. The Web API emerged precisely in its finding: how to make the MVC system work without relying on the presentation layer?

what would be the best way to work with Views in such a way that I could create the systems with a user experience closer to those provided by the previously mentioned technologies while maintaining the MVC development standard with Razor?

First, developing methods that respond to the new presentation layer within the MVC system itself. Then migrating this structure to a purely web API system. I have a course that explains how to do this, if interested (the menu is the same).

That is, given a method that normally returns a View:

public ActionResult DevolveView()
{
    ...
    return View(objetos);
}

You make a copy of this method in order to return a structured JSON:

public JsonResult DevolveView()
{
    ...
    return Json(objetos, JsonRequestBehavior.AllowGet);
}

And then create a Controller separately for these new methods using JSON:

public MeuControllerApi : ApiController
{
    public IHttpActionResult DevolveView()
    {
        ...
        return Ok(objetos);
    }
}

This is just a suggestion. The ideal is to make a denser tutorial to understand how this migration works.

  • Gypsy, thank you for the answers to my questions. Regarding your examples it became very clear how I should serve the view, my problem is exactly how to present a View using Razor and getting the return in Json? I know that Jquery is able to help me, but as far as I could understand the "date" return does not directly fill in the View inputs. I speak a lot in View with Razor , because currently it is the one I know most in the scenario . Net MVC. Regarding your course, is it presented in video or only in book? I think little use of the power of Razor and your book can help me!

  • About your first question, you need to use a framework more robust than jQuery to achieve. My suggestions are Aurelia, React and Angular 2, in this order.

  • About my course, the lessons are done by Hangouts and recorded on video by me. The student gets a copy to watch whenever he wants. There is the book that serves as support material for the content presented in Hangout. The course does help a lot in the use of Razor.

  • Hello Gypsy, apparently to achieve what desire has no other way out than to leave for an MVVM Framework, but these frameworks do not make me stop using a view with Razor? Another thing, considering the order presented in your last reply, should I start with Aurelia? Thank you again for the strength!!!

  • Gypsy, regarding your course, I can get all the information on the link http://www.codingcraft.com.br - Price, payment term among others?

  • "Hello Gypsy, apparently to achieve what desire has no other way out than to leave for an MVVM Framework, but these frameworks do not make me stop using a view with Razor?" exactly. You have no HTML generation on the server. All the server generates is JSON and the browser Javascript interprets this JSON to generate a dynamic screen result.

  • "Another thing, considering the order presented in your last reply, should I start with Aurelia?" Yes.

  • About the course, we will continue the contact by email, Facebook or Twitter. See my profile with all the contact possibilities I have. Anyway, yes, the price and payment term are the same as the link.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.