asp.NetMVC 3 modelリモート検証
5266 ワード
だからMVC 3と言うのは、このリモート検証の方法がMVC 3にしかないからです.だから使いたい兄弟たちは次のMVC 3に行くしかない.もちろんframework 4をインストールしなければならない.0.
あまり言わないで、コードをつけます:
モデル層:
C層:
V層:
やってみよう
あまり言わないで、コードをつけます:
モデル層:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
// ,
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace TestMVC.Models
{
public class Eorgans
{
/// <summary>
///
/// </summary>
[Range(0, 9,ErrorMessage=" 0-9 ")]
public int sortId { get; set; }
/// <summary>
///
/// </summary>
[Required(ErrorMessage = " ")]
// 。 , , Action ,
// ( RemoteAttribute ,
// :public RemoteAttribute(string action, string controller, string areaName);
// Area 。
[Remote("ValName", "Organ", ErrorMessage = " ")]
public string or_Name { get; set; }
/// <summary>
/// sortcode
/// </summary>
[StringLength(4), Required(ErrorMessage=" 4 ")]
public string sortCode { get; set; }
/// <summary>
///
/// </summary>
public string or_id { get; set; }
}
}
C層:
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestMVC.Models;
namespace TestMVC.Controllers
{
public class OrganController : Controller
{
//
// GET: /Organ/
DBClass db = new DBClass();
public ActionResult Index()
{
IEnumerable<Eorgans> list = db.organsList();
return View(list);
}
public ActionResult AddOrgan()
{
Eorgans eo = new Eorgans();
return View(eo);
}
[HttpPost]
public ActionResult AddOrgan(Eorgans eo)
{
if (ModelState.IsValid)
{
string tempstr = eo.or_Name + eo.sortCode + eo.sortId;
return Content(tempstr);
}
return View(eo);
}
// , , httpget , post , json , bool 。
// ,
// organName name
[HttpGet]
public ActionResult ValName(string or_Name)
{
return Json(or_Name!= "chen", JsonRequestBehavior.AllowGet);
}
}
}
V層:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestMVC.Models.Eorgans>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
AddOrgan
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>AddOrgan</h2>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.sortId) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.sortId) %>
<%= Html.ValidationMessageFor(model => model.sortId) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.or_Name) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.or_Name) %>
<%= Html.ValidationMessageFor(model => model.or_Name) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.sortCode) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.sortCode) %>
<%= Html.ValidationMessageFor(model => model.sortCode) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.or_id) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.or_id) %>
<%= Html.ValidationMessageFor(model => model.or_id) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
やってみよう