When I first learned mvc3, I encountered many problems during the learning process. Now I have summarized the problems and will update them later. It is convenient for new users like me ..
1. For the handwritten model class, the table object cannot be found due to EF execution errors.
[Tableattribute ("Productentity")]
Public ClassProductentity {}
2. Load different layout and add the logic to _ viewstart. cshtml.
@{If(Request. url. absoluteuri. Contains ("Manage"))
{
Layout ="~ /Views/shared/_ mlayout. cshtml";
}Else{
Layout ="~ /Views/shared/_ layoutlogin. cshtml";
}
}
3. Set dynamic URLs for image Images
A. Detail/creat/Edit page:
@ Model taiqiu. Models. productentity
<IMG id ="Preview"Src ="@ Html. displayfor (model => model. picurl)"/>
B. List page:
@ Model ienumerable <taiqiu. Models. productentity>
@ Foreach (VaRItemInModel)
{
<IMG src ="@ Item. picurl"Alt ="@ Item. Title"/>
}
4. User Logon/Permissions
//After the user is verified, the user is written into the cookie
System. Web. Security. formsauthentication. setauthcookie (_ user,False);
//Add authorize to the backend controller. If you can configure users/role
[Authorize (users/role = allow account/role)]
Public ClassManagecontroller: controller {}
Form Verification in the configuration file
<AuthenticationMode= "Forms">
<FormsLoginurl= "~ /Login /"Timeout= "2880" />
</Authentication>
5. Configure mvc3 in IIS6
System. Web. Razor, system. Web. MVC, etc. Copy the corresponding DLL in the development environment to the binfile on the server.
6. control style settings in view
@ Html. textareafor (model => model. infor, new {style = "width: 800px; Height: 400px "})
Or
@ Html. textareafor (model => model. infor, new {@ class = style name })
7. Set rows and columns for textarea (2nd parameters are rows and 3rd parameters are columns)
@ Html. textareafor (model => model. fileinfo, 5, 5, new {style = "width: 300px; Height: 100px ;"})
8. Upload the file. Pay attention to the bold red part.
ViewCode:
@ Using (html. beginform ("actionname", "cotrollername", formmethod. Post, new {Enctype = "multipart/form-Data"}))
{
<InputType= "File"Name= "Filepath"ID= "Filepath" />
}
Controller code:
Httppostedfilebase file = request. Files [0]AsHttppostedfilebase;
If(File. filename! ="")
{
//Code
}
9. foreach, using the dynamic characteristics of viewbag
Controller code:
VaRRecommendporduct = PE. productentity. Where (A => A. Recommend). Take (5);
Viewbag. rplist = recommendporduct;
ReturnView ();
View Code :(Note: No code is prompted when viewbag is used.)
@ Foreach (VaRItemInViewbag. rplist)
{
<DivClass="Rught_cptxt2">
<Ul>
<LiClass="Rught_cpimg1"> <A href ="#">
<IMG src ="@ Item. picurl. Trim ()"Border ="0"/> </A> </LI>
<LiClass="Rught_cptxt1"> <A href ="#"> @ Item. Title </a> </LI>
</Ul>
</Div>
}
10. dropdownlist binding
Controller code:
//Category
PublicSelectlist getsl ()
{//Here, you can use EF to read static data from the database.
List <selectlistitem> List =NewList <selectlistitem> {
NewSelectlistitem () {value ="0", Text ="News"},
NewSelectlistitem () {value ="1", Text ="TechnologyArticle"}
};
Return NewSelectlist (list,"Value","Text");
}
PublicActionresult create ()
{
Viewbag. DDL = getsl ();
ReturnView ();
}
View code:
// The value in viewbag corresponding to the quotation mark
@ Html. dropdownlist ("DDL ")
Alternatively, use a strong type (put the getsl () Static Method in the class and call it directly .)
@ Html. dropdownlistfor (model => model. istechnology, getsl (), "select ")
11. Query
A. SQL is supported in ef4.1:
Dbset<Blogmaster>Set=Context. Set<Blogmaster>();
List<Blogmaster>List=Set. Sqlquery ("Select * From blogmaster where userid = '3'"). Tolist ();
B. Use the LINQ to Entity Framework:
Demodbentities Context=NewDemodbentities ();
Dbset<Blogmaster>Set=Context. Set<Blogmaster>();
VaR result=From uInSet. Tolist ()
WhereU. userid=3
Select U;
C. Use labmda to query:
VaR list=Set. Where (o=>O. userid=3);
VaR listgroup=Set. Groupby (o=>O. userid );
12. Output HTML tags
Passing Parameters
@ Html. Action ("Header", "common", new routevaluedictionary {"controllername", controllername }})
@ Html. Raw (content)