asp.net MVC introduction 7, Hellper and data submission and binding __.net

Source: Internet
Author: User
Tags eval actionlink

Reprint Address: http://www.cnblogs.com/QLeelulu/archive/2008/10/05/1303991.html

asp.net MVC provides a number of Hellper methods, Hellper is a way to generate HTML code that makes it easier for us to write HTML code (some friends prefer to write HTML code directly). We can also use the. NET 3.5 extension method to write our own hellper.

For example: <% = Html.ActionLink ("First page", "Index", "Home")%>

The generated HTML code is: <a href= "/home/index" > Home </a>. One thing to note here is that the URLs generated by the Html.ActionLink () and Url.link () methods are related to the sequence of route you configure in Global.asax.

I will not elaborate on the use of the various methods of hellper, and you can refer to the heavy pawnage asp.net Mvc Framework series articles.

Let's implement the setting mentioned earlier to set the basic settings for the blog. Let's take a look at the code on the views/admin/setting.aspx page: < p >
< label for = "name" > Blog name </label >
<% = Html.textbox ("Name")%>
<% = Html.validationmessage ("Name")%>
</p >
< p >
< label for = "Description" > Blog Simple description </label >
<% = Html.textarea ("Description")%>
<% = Html.validationmessage ("Description")%>
</p >
< p >
< label for = "Postsperpage" > number of logs displayed per page </label >
<% = Html.textbox ("Postsperpage")%>
<% = Html.validationmessage ("Postsperpage")%>
</p >

Our setting action method is written like this:

Note that we are return View (blogsettings.instance) and Viewdata.model passed the blogsettings.instance. Then run it for a look:

Notice that the textbox above has a value. We used Html.textbox ("Name") and did not specify a value. So how does this value automatically bind up? Notice that the front in the action we are return View (blogsettings.instance); The parameter "name" of Blogsettings.instance,blogsettings.instance.name and Html.textbox ("name") was passed to Viewdata.model with the same name. The Html.textbox () method calls the Viewdata.eval () method at the time of the call, so the value is automatically bound up. Viewdata.eval () can use "." In the previous article. expressions, Html.textbox () can also use the "." An expression, such as: Html.textbox ("Post.title") can also automatically bind the ViewData.Model.Post.Title value up.

Here we will commit the value to the action and save it. Our form uses a post back server: < form id = "fields" action = "<%=url.action (" Setting "," Admin ")% >" method= "Post >

Then we handle it in action: [ActionName ("Setting"), Acceptverbs ("POST")]
Public ActionResult SaveSetting ()
{
Of course you can take the value
String name = request.form["Name"];

But we have more simple:
Try
{
Updatemodel (Blogsettings.instance, new [] {"Name", "Description", "Postsperpage"});
}
Catch
{
Return View (blogsettings.instance);
}

You may also need to validate your custom business logic here

BlogSettings.Instance.Save ();
Return ShowMsg (New List < string > () {"Modify settings succeeded"});
}

As shown in the code above, we can use the Updatemodel () method to assign directly to the specified object the value of the form form that is post to the server. The validation of the business logic for the submitted data here is a lot to discuss, there are a lot of foreign articles to discuss this, we can search and see.

Write so much for the time being, and add to what you think. Enjoy. Post by Q.lee.lulu.

This article's blog Program Sample code: 4mvcblog_7.rar

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.