MVC Linq to sql 查出来的数据怎样显示在View 页

yujinliang 2009-12-01 02:08:23
HomeController.cs

public ActionResult Edit(int? id)
{
ViewData["id"] = id;
var dataContext = new MovieDataContext();

var movies = from m in dataContext.Movies
where m.id == 1
select m;
return View(movies);
}

VIEW :


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="test.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Edit</h2>
<%= Html.ValidationSummary() %>
<% using (Html.BeginForm())
{%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Title">
ProductName:</label>
<%= Html.TextBox("Title", ViewData.Model)%>
<%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>




=ViewData.Model

这样写输出的是SQL语句,

我想取里面的字段 =ViewData.Model.id , = ViewData.Model.Title 里面没有这俩个属性是为什么呢?

怎么样显示在View 页呀。

...全文
413 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xibo_826 2010-05-27
  • 打赏
  • 举报
回复
写错了,不是不要继承,是要指定类型
Inherits="System.Web.Mvc.ViewPage<MyObject>"
xibo_826 2010-05-27
  • 打赏
  • 举报
回复
页面继承的类改下就行了
就是这句 Inherits="System.Web.Mvc.ViewPage"
不要继承System.Web.Mvc.ViewPage,用你自己的类
scdn8311 2010-05-25
  • 打赏
  • 举报
回复
就是啊,用强类型就OK了
staywithc 2010-05-24
  • 打赏
  • 举报
回复
可以生成强类型视图,直接使用Model.属性取值即可
dengzhoujie 2010-05-08
  • 打赏
  • 举报
回复
学习。
flyerwing 2010-05-04
  • 打赏
  • 举报
回复
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<mvcapp.moduls>" %>
viewdata["key"]
viewdata.key
这样都成!
yujinliang 2009-12-01
  • 打赏
  • 举报
回复
ASP.net MVC 讨论群 欢迎加入,1243466
yujinliang 2009-12-01
  • 打赏
  • 举报
回复
我加你了。
yujinliang 2009-12-01
  • 打赏
  • 举报
回复
谢谢,我已经解决了,我用了VS2008 把 view 页面删除,

然后在Controller 页面,右键。

Addview

Create a strongly-typed view 前面点上√

view data class 选的是 test.Models.Movies 然后点ADD 之后就好用了。

好像是我以前的 view 页面,没有选 test.Models.Movies 什么强类型的。
caotoulei 2009-12-01
  • 打赏
  • 举报
回复
共同学习.net MVC哈,可以站内联系我,或者q284890303
我理解你是想拿Movie Model里的id 和 Title, 你应该用movie.id 和 movie.Title
在你的View上 你要用大概这样一段
foreach (Movie movie in ViewData.Model)
{
movie.id.....
movie.Title....
}



贴一个我写的类似的页面,帮助理解。。。中间有一些jQurey的东东,你忽略不看。。。
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="TPIAR.Web.Models" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$().ready(function() {
$("img.email").click(function() {
$.post(
"/Home/RandomDetailsPopupView",
{ id: $(this).attr("longdesc") },//todo: how to use other attr to pass value(except "longdesc") to HomeController...?
function(htmlResult) {
$("#RandomModal").remove();
$("#container").append(htmlResult);
$("#RandomModal").dialog();
}
);
});
});
</script>
<script type="text/javascript">
$().ready(function() {
$("img.note").click(function() {
$.post(
"/Home/NotePopupView",
{ id: $(this).attr("longdesc")},
function(htmlResult) {
$("#Note").remove();
$("#container").append(htmlResult);
$("#Note").dialog().data("width.dialog", 550);
}
);
});
});
</script>
<%-- <script type="text/javascript">
$().ready(function() {
$("img.note").hover(function(){
$(this);
});
});--%>


</script>

<div id="container">
<link rel="stylesheet" type="text/css" href="../../Css/default.css"/>
<% foreach (InformationRequest infoReq in (IEnumerable)ViewData.Model)
{
var divName = "";
if (!infoReq.IsCompleted)
{
if (Convert.ToInt32(((DateTime.Now) - (infoReq.RequestDate)).TotalDays) > 9)
divName = "err";
else if ((Convert.ToInt32(((DateTime.Now) - (infoReq.RequestDate)).TotalDays) < 9)
&& (Convert.ToInt32(((DateTime.Now) - (infoReq.RequestDate)).TotalDays) > 5))
divName = "help";
else
divName = "info";
%> <ul>
<div class= "<%=divName%>" >
<table style="width:100%;">
<tr>
<td class="fontGray">
Name:<%=Html.Encode(infoReq.FirstName)%> <%=infoReq.LastName%></td>
<td class="fontGray">
Request Time: <%=infoReq.RequestDate%></td>
<td >
Email<img class="email" alt='Email.' longdesc="<%=infoReq.RequestId%>" src="../../i/email.png"/> </td>
<td>
<%=Html.ActionLink("Close", "Close",
new {Controller = "Home", Action = "Close", id = infoReq.RequestId})%></td>
</tr>
</table>
<li>Address:<%=infoReq.Address%>, <%=infoReq.City%>, <%=infoReq.State%>, <%=infoReq.PostalCode%></li>
<li>Telephone:<%=infoReq.TelephoneNumber%></li>
<li>Fax:<%=infoReq.FaxNumber%> </li>
<li>Company:<%=infoReq.CompanyName%> </li>
<li>Email:<%=infoReq.EmailAddress%></li>
<li>Request Summary: <%=infoReq.RequestSummary%></li>
<%
var strIdAndNote = Convert.ToBase64String(Encoding.Unicode.GetBytes(infoReq.RequestId + "+" + infoReq.Notes));
%>
<li>Action: <img class="note" alt="Note is here." longdesc= "<%=strIdAndNote%>" src="../../i/note.jpg" /><ul id="cNote" class="fontGray"><%=infoReq.Notes%></ul></li>
</div>
</ul>
<%
}
}
%>
</asp:Content>

另外干嘛要这种“public ActionResult Edit(int? id)”, OOP的精神咧,呵呵。

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧