Dynamicdata for Asp.net MVC message this example is updated later

Source: Internet
Author: User

I am not planning to create another Edit page for update, but intend to use the existing create page to implement the UPDATE function.

Although this may not comply with its semantics, I still want to do so, because ASP at that time, we all put the addition, deletion, modification, and query on a page and only use parameters to distinguish between them.

For example:

Article? M = add

Article? M = modfiy

Article? M = Delete

Due to the popularity of controls, the details are concealed by Asp.net. Many friends no longer use querystring or hidden forms to control functions, or those who have never touched ASP have never thought about this, the addition and deletion functions are provided for controls such as gridview.

But under Asp.net MVC, should we re-understand such basic things?

 

A friend just asked me a question about how to differentiate Add/edit using querystring, so I feel a lot of feelings. I'm sorry to say a few words.

 

In order not to break the structure mentioned above, I can only add an empty parameter of ID for create. If ID is empty, it is added, if a value with an ID is passed in, it is edited.

OK to start.

First, add a modified connection to the list.

  <! -- Display list start -->  <% Foreach (var a in viewdata. Model ){%>  <  Fieldset  >  <  Legend  >  <% Html. renderdynamicfield (a, "title "); %>  </  Legend  > //.... <% = Html. actionlink ("modify", "CREATE", new {id = A. ID}, null)%>  </  Fieldset  >  <% } %> 

Here we passed the ID

On the create page, modify the View:

              <  Form   Method = "Post"  >                  <% Html. renderdynamicentity ("art", viewdata. Model, viewdata. Model = NULL? Databoundcontrolmode. insert: databoundcontrolmode. Edit ); %>                  <  Input   Type = "Submit"   />              </  Form  > 

In this way, the view page can automatically generate the added or modified form, and its judgment is based on whether viewdata. model has a value.

We determine the ID in the Controller. If so, assign a value to viewdata. model.

         /// <Summary>          /// Page for display          /// </Summary> [Acceptverbs (httpverbs. Get)] Public Actionresult create ( Long ? ID ){ If (Id. hasvalue ){ Using (Var db = New Dmvcdatadatacontext () {var art = dB. Articles. Where (C => C. ID = ID. Value). firstordefault (); Return View (ART );}}Return View ();}

Similarly, we make a judgment on its processing action.

  /// <Summary>          /// Page for receiving data, which is accessed only by post  /// </Summary>  /// <Param name = "ID"> </param>  /// <Param name = "art"> </param>  /// <Returns> </returns> [Acceptverbs (httpverbs. Post)] Public Actionresult create ( Long ? ID, articles art ){ Try {If (Viewdata. modelstate. isvalid ){ Using (Var db = New Dmvcdatadatacontext ()){ If (Id. hasvalue ){ // Modify data when an ID exists VaR A = dB. articles. where (C => C. id = ID ). firstordefault ();. title = art. title;. body = art. body;. author = art. author;. addtime = art. addtime ;} Else { // Add data if no ID exists DB. Articles. insertonsubmit (ART);} dB. submitchanges (); Return Redirecttoaction (" List ");}}} Catch (Exception ex) {modelstate error = New Modelstate (); error. errors. Add (Ex); viewdata. modelstate. Add (" Category ", Error );} Return View ();}

In this way, we have a page with both modification and addition functions.

 

Example download: http://files.cnblogs.com/chsword/DMvc.rar

 

Dynamicdata for Asp.net MVC message this example is updated later

Dynamicdata for Asp.net MVC message this instance creates. deletes. Data Verification

Dynamicdata for Asp.net MVC message. This example provides the preparation work and displays a list of articles.

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.