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 ();}