当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

MVC下读取XML生成动态表单的例子

问题的提出:在MVC下动态表单怎么生成?
好的,我们来看下面的例子:

接着上次的MVCMembership来讲

我们首先添加一个目录XML。然后添加View:

1.checkXml.aspx 用来检查我们输入的XML格式(利用XSD检查)
2.New.aspx 用来新增XML表单的
3.Show.aspx 用来显示XML表单的
4.ShowResult.aspx 用来显示XML表单提交的结果的
一、数据库结构
要用到动态的表单,这里我们利用Sqlserver2005的XML类型来保存,建表的SQL如下:

use Test
/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2005                    */
/* Created on:     2009/5/8 7:56:50                             */
/*==============================================================*/
if exists (select 1
from  sysindexes
where  id    = object_id('XMLFORM')
and   name  = 'IDX_XML'
and   indid > 0
and   indid < 255)
drop index XMLFORM.IDX_XML
go
if exists (select 1
from  sysindexes
where  id    = object_id('XMLFORM')
and   name  = 'IDX_ID'
and   indid > 0
and   indid < 255)
drop index XMLFORM.IDX_ID
go
if exists (select 1
from  sysobjects
where  id = object_id('XMLFORM')
and   type = 'U')
drop table XMLFORM
go
/*==============================================================*/
/* Table: XMLFORM                                               */
/*==============================================================*/
create table XMLFORM (
ID                   int                  identity,
FORMXML              xml                  not null,
constraint PK_XMLFORM primary key (ID)
)
go
declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description',
'XMLFORM',
'user', @CurrentUser, 'table', 'XMLFORM'
go
/*==============================================================*/
/* Index: IDX_ID                                                */
/*==============================================================*/
create unique index IDX_ID on XMLFORM (
ID ASC
)
go
/*==============================================================*/
/* Index: IDX_XML                                               */
/*==============================================================*/
create PRIMARY XML INDEX IDX_XML on XMLFORM (
FORMXML
)
go好了我们建了一个名为XMLForm的表,其中ID自增,FORMXML为我们需要的XML表单的内容

二、编写XML的Controller
XMLController.cs

主要的action

1.New

image

 imageimageimage
 [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
public ActionResult New(string xmlContent)
{
System.Threading.Thread.Sleep(2000);    //模拟提交等待
xmlContent = Server.UrlDecode(xmlContent);
if (!string.IsNullOrEmpty(xmlContent))
{
//if (!CheckXML(xmlContent, out strError)) //服务器端检测,如果用了ajax检测,就没必要了
//{
//    ModelState.AddModelError("_FORM",strError); 
//    return View();
//}
XMLFormDataContext db = new XMLFormDataContext();
TransactionOptions opt = new TransactionOptions();
ViewData["xmlContent"] = xmlContent;
opt.IsolationLevel = IsolationLevel.Serializable;
using (TransactionScope tran = new TransactionScope(TransactionScopeOption.RequiresNew, opt))
{
XMLFORM f = new XMLFORM();
try
{
f.FORMXML = XElement.Parse(xmlContent);
db.XMLFORMs.InsertOnSubmit(f);
db.SubmitChanges();
var id = db.XMLFORMs.Max(p => p.ID);
ViewData["result"] = "success";
ViewData["id"] = (int)id;
tran.Complete();
return View();
}
catch
{
ViewData["result"] = "failure";
ModelState.AddModelError("_FORM", "envalid xml format");
return View();
}
}
}
else
return View();
}XML:<Form Title="testForm">
<Input Text="ID" Name="txtID" Type="TextBox" Required="true"></Input>
<Input Text="Name" Name="txtName" Type="TextBox" Required="true"></Input>
<Input Text="Job" Name="txtJob" Type="TextBox" Required="false"></Input>
<Input Text="Validate" Name="btnValidate" Type="Button"></Input>
<Input Text="Commit" Name="btnComit" Type="Button"></Input>
</Form>
注意:我们使用了事务,所以不要忘了最后要提交。我就是忘了,找了3天错误  

2.Show

image
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Show(int? ID)
{
int nId = 0;
XElement doc = null;
if (int.TryParse(Request["id"], out nId))
{
try
{
XMLFormDataContext db = new XMLFormDataContext();
var q = from f in db.XMLFORMs
where f.ID == nId
select f.FORMXML
;
if (q.Count() > 0)
{
foreach (var qq in q)
{
doc = qq;
}
ViewData["xml"] = doc;
}
else
{
ModelState.AddModelError("_FORM", "Not Exists");
}
ViewData["id"] = nId;
}
catch (Exception e)
{
ModelState.AddModelError("_FORM", e.Message);
}
}
else
{
ModelState.AddModelError("_FORM", "Miss ID");
}
return View();
}
注意这里Show.asp的写法.不能用<input .../>直接输出控件字符串,

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接