mvc中实现更新数据操作时遇到的问题
来源:互联网 发布:springboot配置mysql 编辑:程序博客网 时间:2024/05/06 03:31
1、mvc中使用富文本控件
除了Page指令中要加上: ValidateRequest="false" 之外,
在Action的特性上也要加上[ValidateInput(false)]
2、文件上传控件必须让Form标签加上: enctype="multipart/form-data" 属性。
在后台可以通过:
HttpPostedFileBase file = this.HttpContext.Request.Files[0];
得到上传的文件对象。
更新操作的Action
<form enctype="multipart/form-data" method="post">
<style>
#author
{
width:300px;
}
#title
{
width:300px;
}
#publisher
{
width:300px;
}
#category
{
width:300px;
}
#aurhorDescription
{
width :600px;
}
#contentDescription
{
width :600px;
}
</style>
<table style="width: 100%;">
<tr>
<td style="width: 102px">
书名
</td>
<td>
<%=Html.TextBox("title", book.Title)%>
</td>
</tr>
<tr>
<td style="width: 102px">
作者
</td>
<td>
<%= Html.TextBox("author", book.Author)%>
</td>
</tr>
<tr>
<td style="width: 102px">
封面
</td>
<td>
<img src='/Images/BookCovers/<%= book.ISBN %>.jpg' />
<input type="file" name = "Cover" />
</td>
</tr>
<tr>
<td style="width: 102px">
ISBN
</td>
<td>
<%= Html.TextBox("isbn", book.ISBN)%>
</td>
</tr>
<tr>
<td style="width: 102px">
出版社
</td>
<td>
<asp:DropDownList ID="publisher" runat="server" DataTextField="Name" DataValueField="ID">
</asp:DropDownList>
<%=Html.Hidden("publisher", book.PublisherId)%>
</td>
</tr>
<tr>
<td style="width: 102px">
分类
</td>
<td>
<asp:DropDownList ID="category" runat="server" DataTextField="Name" DataValueField="ID">
</asp:DropDownList>
<%=Html.Hidden("category", book.CategoryId)%>
</td>
</tr>
<tr>
<td style="width: 102px">
目录
</td>
<td>
<FTB:FreeTextBox ID="TOC" runat="server" >
</FTB:FreeTextBox>
<%=Html.Hidden("TOC")%>
</td>
</tr>
<tr>
<td style="width: 102px">
作者简介
</td>
<td>
<%= Html.TextArea("aurhorDescription", book.AurhorDescription)%>
</td>
</tr>
<tr>
<td style="width: 102px">
摘要
</td>
<td>
<%= Html.TextArea("contentDescription", book.ContentDescription)%>
</td>
</tr>
</table>
<input type="submit" value="提交" onclick="return getData()" />
<script language ="javascript">
function getData() {
document.getElementById("category").value = document.getElementById("ctl00_MainPlaceHolder_category").value;
document.getElementById("publisher").value = document.getElementById("ctl00_MainPlaceHolder_publisher").value;
document.getElementById("TOC").value = document.getElementById("ctl00_MainPlaceHolder_TOC").value;
return true;
}
</script>
</form>
除了Page指令中要加上: ValidateRequest="false" 之外,
在Action的特性上也要加上[ValidateInput(false)]
2、文件上传控件必须让Form标签加上: enctype="multipart/form-data" 属性。
在后台可以通过:
HttpPostedFileBase file = this.HttpContext.Request.Files[0];
得到上传的文件对象。
更新操作的Action
[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
public ActionResult BookDetail(int id,string title,string author,
string Cover,string isbn,string publisher,string category,
string TOC,string aurhorDescription,string contentDescription) {
string sql = @"UPDATE Books SET Title=@Title,author=@author,
AurhorDescription = @authorDescription, contentDescription=@contentDescription,
PublisherId = @PublisherId, CategoryId = @CategoryId, TOC = @TOC
WHERE ID=@ID";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyBookShop;uid=sa");
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
com.Parameters.Add("@author", SqlDbType.VarChar).Value = author;
com.Parameters.Add("@authorDescription", SqlDbType.VarChar).Value = aurhorDescription;
com.Parameters.Add("@contentDescription", SqlDbType.VarChar).Value = contentDescription;
com.Parameters.Add("@PublisherId", SqlDbType.VarChar).Value = publisher;
com.Parameters.Add("@CategoryId", SqlDbType.VarChar).Value = category;
com.Parameters.Add("@TOC", SqlDbType.VarChar).Value = TOC;
com.Parameters.Add("@ID", SqlDbType.VarChar).Value = id;
con.Open();
com.ExecuteNonQuery();
con.Close();
HttpPostedFileBase file = this.HttpContext.Request.Files[0];
string path = Server.MapPath("~/Images/BookCovers/" + isbn + ".jpg");
file.SaveAs(path);
return this.RedirectToAction("BookList");
}
view中的代码:[ValidateInput(false)]
public ActionResult BookDetail(int id,string title,string author,
string Cover,string isbn,string publisher,string category,
string TOC,string aurhorDescription,string contentDescription) {
string sql = @"UPDATE Books SET Title=@Title,author=@author,
AurhorDescription = @authorDescription, contentDescription=@contentDescription,
PublisherId = @PublisherId, CategoryId = @CategoryId, TOC = @TOC
WHERE ID=@ID";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyBookShop;uid=sa");
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
com.Parameters.Add("@author", SqlDbType.VarChar).Value = author;
com.Parameters.Add("@authorDescription", SqlDbType.VarChar).Value = aurhorDescription;
com.Parameters.Add("@contentDescription", SqlDbType.VarChar).Value = contentDescription;
com.Parameters.Add("@PublisherId", SqlDbType.VarChar).Value = publisher;
com.Parameters.Add("@CategoryId", SqlDbType.VarChar).Value = category;
com.Parameters.Add("@TOC", SqlDbType.VarChar).Value = TOC;
com.Parameters.Add("@ID", SqlDbType.VarChar).Value = id;
con.Open();
com.ExecuteNonQuery();
con.Close();
HttpPostedFileBase file = this.HttpContext.Request.Files[0];
string path = Server.MapPath("~/Images/BookCovers/" + isbn + ".jpg");
file.SaveAs(path);
return this.RedirectToAction("BookList");
}
<form enctype="multipart/form-data" method="post">
<style>
#author
{
width:300px;
}
#title
{
width:300px;
}
#publisher
{
width:300px;
}
#category
{
width:300px;
}
#aurhorDescription
{
width :600px;
}
#contentDescription
{
width :600px;
}
</style>
<table style="width: 100%;">
<tr>
<td style="width: 102px">
书名
</td>
<td>
<%=Html.TextBox("title", book.Title)%>
</td>
</tr>
<tr>
<td style="width: 102px">
作者
</td>
<td>
<%= Html.TextBox("author", book.Author)%>
</td>
</tr>
<tr>
<td style="width: 102px">
封面
</td>
<td>
<img src='/Images/BookCovers/<%= book.ISBN %>.jpg' />
<input type="file" name = "Cover" />
</td>
</tr>
<tr>
<td style="width: 102px">
ISBN
</td>
<td>
<%= Html.TextBox("isbn", book.ISBN)%>
</td>
</tr>
<tr>
<td style="width: 102px">
出版社
</td>
<td>
<asp:DropDownList ID="publisher" runat="server" DataTextField="Name" DataValueField="ID">
</asp:DropDownList>
<%=Html.Hidden("publisher", book.PublisherId)%>
</td>
</tr>
<tr>
<td style="width: 102px">
分类
</td>
<td>
<asp:DropDownList ID="category" runat="server" DataTextField="Name" DataValueField="ID">
</asp:DropDownList>
<%=Html.Hidden("category", book.CategoryId)%>
</td>
</tr>
<tr>
<td style="width: 102px">
目录
</td>
<td>
<FTB:FreeTextBox ID="TOC" runat="server" >
</FTB:FreeTextBox>
<%=Html.Hidden("TOC")%>
</td>
</tr>
<tr>
<td style="width: 102px">
作者简介
</td>
<td>
<%= Html.TextArea("aurhorDescription", book.AurhorDescription)%>
</td>
</tr>
<tr>
<td style="width: 102px">
摘要
</td>
<td>
<%= Html.TextArea("contentDescription", book.ContentDescription)%>
</td>
</tr>
</table>
<input type="submit" value="提交" onclick="return getData()" />
<script language ="javascript">
function getData() {
document.getElementById("category").value = document.getElementById("ctl00_MainPlaceHolder_category").value;
document.getElementById("publisher").value = document.getElementById("ctl00_MainPlaceHolder_publisher").value;
document.getElementById("TOC").value = document.getElementById("ctl00_MainPlaceHolder_TOC").value;
return true;
}
</script>
</form>
0 0
- mvc中实现更新数据操作时遇到的问题
- 学习spring mvc 时遇到的问题(持续更新)
- HIVE中数据更新(update)操作的实现
- vue2中src实现数据绑定遇到的问题
- db2遇到的问题(更新中)
- 关于.net MVC中主视图和分部视图的数据共享遇到的问题
- MySQL使用中遇到的问题及日常操作累积(持续更新)
- MVC模式实现用户登录练习中遇到的问题和需要注意的细节
- CoreData-实现数据永久性保存时遇到的问题
- Solrj实时更新遇到数据变化的问题
- 文件操作中遇到的问题
- Android ListView的中的数据更新时遇到的一些问题的总结
- 表更新时遇到的问题
- widget 更新时遇到的问题
- centos更新yum时遇到的问题
- 使用 通用mapper时 根据主键进行 删除 更新操作 遇到的问题
- 工作中遇到的问题,持续更新中
- 数据迁移中遇到的问题
- C#异步线程(二)Thread类
- Linux connection to " " refused by server解决方法
- MVC中小试了一下Jquery
- 用JQuery实现对ASP.NET TreeView控件的级连选中
- 凯歌[转自:威继光]
- mvc中实现更新数据操作时遇到的问题
- 对RegisterClientScriptResource使用中的一点小问题探究了一下
- 理化所合作研发出世界首个自主运动的可变形液态金属机器
- Jquery+JSon实现的Asp.net无刷新分页控件
- 关于索引的一道经典面试题
- 离职or not 离职
- list和str的反转
- java参数传递时到底是值传递还是引用传递
- 一个挺有意思的小问题Html控件和Web控件