个性化阅读
专注于IT技术分析

asp.net mvc模型绑定

点击下载

本文概述

模型绑定是我们将模型绑定到控制器和视图的过程。这是将发布的表单值映射到.NET Framework类型并将类型作为参数传递给action方法的一种简单方法。它充当转换器,因为它可以将HTTP请求转换为传递给操作方法的对象。

在这里,我们创建一个示例,其中一个简单的模型与视图和控制器绑定。我们正在创建具有某些属性的Student模型。这些属性将用于创建表单字段。


建立模型

右键单击“模型”文件夹,然后添加一个类以创建新模型。

该文件包含一些默认代码,但是我们已经向其中添加了一些属性。模型看起来像这样:

// Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplicationDemo.Models
{
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Contact { get; set; }
    }
}

创建一个控制器

创建模型后,现在让我们为该类创建一个控制器。右键单击Controller文件夹,然后添加控制器类。

添加后,它提供以下预定义的代码。

// StudentsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class StudentsController : Controller
    {
        // GET: Students
        public ActionResult Index()
        {
            return View();
        }
    }
}

创建一个视图

要创建视图,请在“索引”操作方法的主体内单击鼠标右键,然后选择“添加视图”选项,将弹出该视图的名称,并将模型附加到该视图中。

添加后,它将在“学生”文件夹中生成一个索引文件,其中包含以下代码。

// Index.cshtml

@model MvcApplicationDemo.Models.Student
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

输出:

执行索引文件时,将产生以下输出。

赞(0)
未经允许不得转载:srcmini » asp.net mvc模型绑定

评论 抢沙发

评论前必须登录!