开发者

How do I use JQuery on a View

At work I have been thrown in the deep end. A customer required some functionality on the web and one of our devs knocked together an ASP.Net webforms project.

When I started, because of constraints I got handed this project. I am not a web dev (I've dabbled) but because of the work I have put into the project, the customer is now looking for more functionality to be built into the web solution.

I would like to take a look at MVC (on my own time). With a view of converting the project to it. I come from a Xaml background so MVx jives with me.

I seem to have most of the basic theory nailed down except for one thing. What is the best way to use JQuery on the view?

Should the controller just pass back semantic data and the view (via JQuery) makes it pretty / unstatic (with help from JQuery UI).

I am confused but from what I can gather is it correct to assume JQuery (in terms of MVC) is like a client side View开发者_Python百科 engine that would handle things like theming, hiding controls, simple validation, popups, etc?


jQuery (and JavaScript in general) can be used to enhance the behaviour of a page. I say enhance, because it should basically work without JavaScript. About 2% of users do not have JavaScript enabled.

CSS should be used to enhance the presentation (or styling) of a page.

The HTML itself is effectively a vector for your raw data which also provides a semantic context. For example, data in a p tag means that that data is a paragraph. Browsers have CSS defined for HTML elements so that by default, the raw data with a semantic context will look vaguely sensible.

So, with regards to "is it correct to assume JQuery (in terms of MVC) is like a client side View engine that would handle things like theming, hiding controls, simple validation, popups, etc?":

Theming should be done using CSS, as it is presentation. The theming you get from the jQuery UI library just gives you some CSS, which is fine.

Hiding / showing content based on events, popups and validation should be done server-side, but JavaScript can be used as well to enhance the behavioural experience.


Pretty much exactly as you said, the controller should pass data to the view which should be rendered using HTML and structured using CSS.

You can then use jQuery to add validation/dynamic effects/ajax etc.


I think you should see Javascript more like an addon to mock things up. For example:

You've got a serverside validation, but you don't want to reload the whole page all the time, then the javascript is good, but it's an addon, because when you don't have a serverside security, someone who hasn't got javascript turned on, or simply doesn't have it, is going to bypass your security easily.

Jquery in this case, is to make those tasks easier to implement.

I know in ASP.net the hiding of controls and the form submits are standard javascript. But it isn't good practice to do this.

Implementing it in MVC is easy as you just need to put it in the scripts map and then load it with a method in your cshtml page.

For more information about how to build your webpage for alot of browsers, i would recommend you searching after progressive enhancement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜