This query was asked by one of students who had attended MVC training in Mumbai past week. Below are the questions and answers , can be useful for some students.
- In lab 3 the one with View data-U mentioned we can’t have server controls, viewstate and code behind but if we use aspx instead of a cshtml page for VIEW then we could have the above 3 is it? The above three attributes are specific to Razor syntax and not MVC right?
Answer :- Yes you can have above code in ASPX as well. They are not really specific to Razor they are MVC codes. You can use them in MVC controller code also. - In lab 3 you used Mytime property without declaring it with type dynamic i.e u directy assigned value to it -” Viewbag. Mytime = datetime. Now. Tostring() “. So how is viewbag able to read the property mytime. Is mytime a keyword or an identifier?I also saw the difference between dynamic and reflection video but it couldn’t help me solve this doubt.
Answers :- Its uses reflection. In other words on runtime it checks for that property. - Does Cshtml page use only Razor syntax?or Can Razor syntax be implemented only in Cshtml page?
Answer :- Razor syntax is for CSHTML. CSHTML is a file extension. - In lab 5 Why did you tag the “Customer” model to “Load View” or “Customer View” and why not “EnterCustomer” view.
- Can we have a model for “EnterCustomer” view as well?
For example :-We want to take many inputs from user but want to display only a few. And save the rest in db so that those properties could be used in some other page.
In such case what should be the model design.
Answer :- Yes you can have for EnterCustomer. Same model can be used with different models. Use inheritance in that case. So create a class with X number of properties. And if you want X+10 more properties then inherit and create a new class. - Also in lab 5 you did not use “ViewData” or “ViewBag” for passing data from controller to view. You did it using the customer object directly. Why is that so?
Answer :- Viewdata and viewbag should be the last resort. They should be used in rare cases where you have hanging properties like date time , some IDs and so on. But when it comes to Business objects like Customer , Supplier you should use strongly typed model object(return view(“Test”,CustomerObject).