Home>

development tools:vs2010 + mssql2005, need to use mvc3.0

Environment configuration

Step 1:Download mvc3 from the official website. Simplified Chinese is provided.Install aspnetmvc3toolsupdatesetup.exe first, then install aspnetmvc3toolsupdatevs11setup.exe

Step 2:Create a new database,Create a test table.Then insert some test data into the table

use [yancomdb]
go
/****** Object:table [dbo]. [Newsentity] Script Date:03/12/2012 22:03:59 ****** /
set ansi_nulls on
go
set quoted_identifier on
go
create table [dbo]. [newsentity] (
 [nid] [int] identity (1,1) not null, [title] [nvarchar] (100) collate chinese_prc_ci_as not null, [information] [text] collate chinese_prc_ci_as null, [time] [datetime] not null constraint [df_newsentity_time] default (getdate ()), constraint [pk_newsentity] primary key clustered
(
 [nid] asc
) with (pad_index=off, ignore_dup_key=off) on [primary]
) on [primary] textimage_on [primary]

Building a list page

The first step:open vs, create a new mvc3 web application,Enter project name and directory

secondStep:Create a newsentity class. This article uses the handwritten entity class (no linqtosql, ef, orm etc.)

[tableattribute ("newsentity")] //This line is very important,Because the MVC framework defaults to db for class names with multiple table names
 public class newsentity
 {
  [key] //Set the primary key
  public int nid {get;set;}
  [stringlength (100)] //Set authentication information
  [required (errormessage="Title cannot be empty")]
  [displayname ("Title")]
  public string title {get;set;}
  [required (errormessage="The text is required")]
  [displayname ("body")]
  public string information {get;set;}
  public datetime time {get;set;}
 }

Step 3:Configure database connection characters,Note that the name here corresponds to the class name created in the next step.

<connectionstrings>
<add name="projectentity" connectionstring="data source=ip;initial catalog=yancomdb;persist security info=true;user id =;password ="
providername="system.data.sqlclient" />
</connectionstrings>

Step 4:Create projectentity class, need to inherit dbcontext

public class projectentity:dbcontext
 {
  public dbset<newsentity>newsentity {get;set;}
 }

Step 5:Create a new controller,

projectentity pe=new projectentity ();
  public actionresult news ()
  {
   try
   {
    var list=pe.newsentity.tolist ();
    return view (list);
   }
   catch (exception e)
   {
    throw e;
   }
  }

Step 6:Right-click on news to create a new view.Check "Create strong type view", select newsentity, and select the bracket module list

After adding, the cshtml code is as follows:

@model ienumerable<taiqiu.models.newsentity>
@ {
 viewbag.title="Background News Management List";
 layout="~/views/shared/_mlayout.cshtml";
}
<h2>
 News list</h2>
<p>
 @ html.actionlink ("Add", "create")
</p>
<table>
 <tr>
  <th width="50px">
   id
  </th>
  <th width="300px">
   title
  </th>
  <th width="150px">
   time
  </th>
  <th>
  </th>
 </tr>
 @foreach (var item in model)
 {
  <tr>
   <td>
    @ html.displayfor (modelitem =>item.nid)
   </td>
   <td>
    @ html.displayfor (modelitem =>item.title)
   </td>
   <td>
    @ html.displayfor (modelitem =>item.time)
   </td>
   <td>
    @ html.actionlink ("edit", "editnews", new {id=item.nid}) |
    @ html.actionlink ("Delete", "deletenews", new {id=item.nid})
   </td>
  </tr>
 }
</table>

At this point, the first list page is complete (no paging is involved,It will be updated later.) About adding,It's easy to modify and delete.

Add controller code:

[httppost]
  [validateinput (false)]
  public actionresult create (newsentity news)
  {
   if (modelstate.isvalid)
   {
    news.time=datetime.now;
    pe.newsentity.add (news);
    try
    {
     pe.savechanges ();
     return redirecttoaction ("news");
    }
    catch (exception e)
    {
     throw e;
    }
   }
   return view ();
  }

Add page:

@model taiqiu.models.newsentity
@ {
 viewbag.title="Add News";
 layout="~/views/shared/_mlayout.cshtml";
}
<h2>
 Add news</h2>
<script src="@ url.content (" ~/scripts/jquery.validate.min.js ")" type="text/javascript"></script>
<script src="@ url.content (" ~/scripts/jquery.validate.unobtrusive.min.js ")" type="text/javascript"></script>
<script src="@ url.content (" ~/scripts/kindeditor/kindeditor.js ")" type="text/javascript"></script>
<script src="@ url.content (" ~/scripts/kindeditor/lang/zh_cn.js ")" type="text/javascript"></script>
<script type="text/javascript">
  var editor;
  kindeditor.ready (function (k) {
   editor=k.create ("textarea [name =" information "]", {
    allowfilemanager:true
   });
  });
</script>
@using (html.beginform ())
{
 @ html.validationsummary (true)
 <fieldset>
  <legend>news</legend>
  <div>
   @ html.labelfor (model =>model.title)
  </div>
  <div>
   @ html.textboxfor (model =>model.title, new {style="width:500px"})
   @ html.validationmessagefor (model =>model.title)
  </div>
  <div>
   @ html.labelfor (model =>model.information)
  </div>
  <div>
   @ html.textareafor (model =>model.information, new {})
   @ html.validationmessagefor (model =>model.information)
  </div>
  <p>
   <input type="submit" value="create" />
  </p>
 </fieldset>
}
<div>
 @ html.actionlink ("Return List", "index")
</div>

Like modifying the page,The controller is slightly modified:

[httppost]
  [validateinput (false)]
  public actionresult editnews (newsentity news)
  {
   if (modelstate.isvalid)
   {
    news.time=datetime.now;
    pe.entry (news) .state=entitystate.modified;//modified
    pe.savechanges ();
    return redirecttoaction ("news");
   }
   return view (news);
  }

Remove the controller code:

public actionresult deletenews (int id)
  {
   var model=pe.newsentity.find (id);
   pe.newsentity.remove (model);
   pe.savechanges ();
   return redirecttoaction ("news");
  }

We are new to mvc3, and this article is just a little accumulation in my learning.There are many bad things,Hope you guys have more ideas.

  • Previous Detailed process of configuring java environment variables in Linux
  • Next Detailed destructors in C ++ programming