Some Preliminary Problems in ASP. NET MVC

Source: Internet
Author: User

1. How can I prevent the controller from returning the view? (For example, only some database operations are performed)

It's easy to define a void-type public method in controller.

Public void deletedata () {using (sqliteconnection conn = new sqliteconnection ("Data Source =" + server. mappath (_ dbfile) {Conn. open (); sqlitecommand cmd = Conn. createcommand (); cmd. commandtext = "delete from products"; cmd. executenonquery ();} // By the way, the transaction is used for SQLite. Code // Using (sqliteconnection conn = new sqliteconnection ("Data Source =" + server. mappath (_ dbfile) // {// Conn. open (); // sqlitetransaction T = Conn. begintransaction (); // try // {// sqlitecommand cmd = Conn. createcommand (); // cmd. commandtext = "insert into products (name, createdate, updatedate) values (@ name, @ createdate, @ updatedate)"; // For (INT I = 0; I <50; I ++) // {// cmd. parameters. clear (); // cmd. parameters. addwithvalue ("name", I. tostring (). padleft (5, '0'); // cmd. parameters. addwithvalue ("createdate", datetime. now); // cmd. parameters. addwithvalue ("updatedate", datetime. now); // cmd. executenonquery (); //} // T. commit (); // catch // {// T. rollback ();//}//}}

The call method is similar to http: // localhost/product/deletedata.
2. How to Make the view return plain text or XML?

Public actionresult gettxt () {return New contentresult () {contenttype = "text/plain", contentencoding = encoding. utf8, content = "Hello world! "};}

If you want to return XML, change text/plain to text/XML.

3. How to pass the datatable to the view?

Although many official tutorials recommend the use of strong-type views, the demand is ever-changing. If you want to pass the datatable to the view, refer to the following:

 
Public actionresult index () {datatable TBL = new datatable (); Using (sqliteconnection conn = new sqliteconnection ("Data Source =" + server. mappath (_ dbfile) {sqlitedataadapter da = new sqlitedataadapter ("select * from products", Conn); DA. fill (TBL); viewdata ["data"] = TBL;} return view ();}

Then you can write in the view as follows:

 
<% Datatable TBL = viewdata ["data"] As datatable; foreach (datarow DR in TBL. Rows) {//...} %>

 4. How to use custom controls (as a data display template )?

Create a partial view (partial view). For details, refer to the following:

 
<% @ Control Language = "C #" inherits = "system. web. MVC. viewusercontrol "%> <% @ import namespace =" system. data "%> <tr> <TD> <% = (viewdata. model as datarow) ["ID"] %> </TD> <% = (viewdata. model as datarow) ["name"] %> </TD> <% = (viewdata. model as datarow) ["createdate"] %> </TD> <% = (viewdata. model as datarow) ["updatedate"] %> </TD> </tr>

You can use the following in the main view:

<Table> <tr> <TH> id </Th> <TH> name </Th> <TH> createdate </Th> <TH> updatedate </Th> </ tr> <% datatable TBL = viewdata ["data"] As datatable; foreach (datarow DR in TBL. rows) {HTML. renderpartial ("~ /Views/product/productdata. ascx ", Dr) ;}%> </table>

 5. How do I jump between pages/views?

There are two situations:

Void-type action (that is, the action of the view not returned in question 1 ):

There is only one method:

 
Response. Redirect ("/product/Index ");

Note:
If it is written as redirect ("/product/Index"), the compilation will also pass, but it will not work at all, because no response is added before. the method is changed to the Redirect method of the controller class. This method has a returned value and must be called with return redirect (). However, this method is of the void type and does not allow return, that's why there is only one method.

The action that normally returns the actionresult:

There are many methods:

 Public actionresult showview1 () {response. redirect ("showview2"); // method 1 // return redirect ("showview2"); // method 2 // return redirecttoaction ("showview2 "); // method 3 return view ("showview2"); // Method 4} public actionresult showview2 () {viewdata ["data"] = "view2"; return view ();} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.