HTML Helpers in ASP.Net MVC

Hi Friends,

Let’s continue the last session and let’s talk more about HTML Helpers. Basically,HTML is a property of the view page base class which is used to create

1) Create Inputs
2) Create Links
3) Create Forms

Many of these helpers are quite intelligent, like we have @Html.EditorFor(model=>model.FirstName) ,so what this editorFor will do here, it will walk through the property definition for that partciluar and then based on that, it will emit that particular control on the page, like if its a string property, then it will emit textbox here, if it would have been a boolean property, then it would have emitted that checkbox control on the page.

so, let me use couple of these property and explain you actual scenario. so, what am willing to do here, is to edit the review of each movie. so what this will do, it will take an action name say “Edit” with some anonymous value, here am passing id just to identify which review am planning to edit. i can pass as many as required anonymous parameters in there.

19th

So, now what am saying to routing engine, that here are some essential values that needs to get passed on the url, these anonymous values are basically additional information which need to get passed in the url. Now, what i need to do here, is create one edit view as shown below in the sample

20th

so, all the basics are fairly simple, this time i just selected from the scaffolding template Edit view.

21th

22th

so, it produced me a fairly simple Edit View. so, now when i build and refresh the page, it should produce me the below page.

23th

now, look at the url, it took the value which i passed in from the Reviews Index page. also, accordingly it emitted the tag. Now, let’s look quite closely in here. for doing the same we need to look @ page source and see what actually happening over here.

24th

so, now if any i change any value and hit save, then in that case it would be the [HttpPost] section of edit part which is going to be triggered. so, am going to change this code little bit which is given with scaffold-ed template.

27th

so, what i have done here is grabbed the value which am going to edit, and to move the values in i have used TryUpdateModel, and what this will do, it will go through a process known as Model Binding. Basically, model binding happens anytime you even have a parameter in an action method. so, when i have id in edit action, so what model binder in MVC will find out that id for me and move it into that for me. so, here model binder is checking that rating property and checking is same exists and in the mean time if any thing fails or any validation error occurs, tryupdatemodel will return false and that instant i don’t want user to save review and if that happens, i’ll return the user on the review page to fix the issue and then save it again. And if every thing goes well, then we just save and return to Reviews page. For, now am just redirecting it Reviews page, since we are not working with database till now.

26th

now, let’s save this rating to 10 and try.

28th

now, let’s try to give some erroneous input and then save the same, like below:-

29th

here, the error message is little bit misleading. obviously, we could easily fix the same. but we’ll see in the later section. Till then stay tuned and happy coding.

Thanks,
Rahul
Happy Coding