最近学习 MVC(linq to entity)
想设计一个话题回复功能,在这涉及到两个表Topic 和Reply,其中Reply中有一个字段为TopicID我想在页面中实现类似 CSDN ,上面是Topic内容,下面是Reply内容,最下方是一个输入框,输入的内容提交保存到Reply里面
这个页面的强类型用的是 Topic(因为要在上面进行显示)这里把View代码贴出来
@model MvcForum.Models.Topic
@{
ViewBag.Title = "Details";
}
<style type="text/css">
hr
{
border: 0;
background-color: #F0F0F0;
height: 1px;
}
</style><script src="../../Scripts/ckeditor/ckeditor.js" type="text/javascript"></script>
<h2>
话题</h2>
@*以下内容显示Topic内容*@
@Html.ActionLink("回复","Reply")
<fieldset style="padding: 10px 0px 10px 0px">
<legend>@Html.DisplayFor(model => model.Subject)</legend>
<br />
<div style="float: left;">
楼主</div>
<div style="border-width: 1px; text-align: right;">
发表于: @Html.DisplayFor(model => model.PubTime)
</div>
<hr />
<div style="margin: 20px 20px 20px 20px;">
@Html.DisplayFor(model => model.Details)
</div>
<hr />
<div style="text-align: right">
引用|管理|对我有用[0]|丢个板砖[0]|top</div>
</fieldset>@*以下内容显示Reply内容*@
@{int i = 1;
foreach (var item in Model.Reply.OrderBy(m=>m.ReplyTime)) {
<br />
<div style="float: left;">@Html.Label(i.ToString())楼</div>
<div style="border-width: 1px; text-align: right;">
回复于: @Html.Label(item.ReplyTime.ToString())
</div>
<hr />
<div>@Html.Label(item.ReplyContent)</div>
<hr />
<div style="text-align: right">
引用|管理|对我有用[0]|丢个板砖[0]|top</div>
i++;
}}
@*通过一个按钮做提交回复,不过这里无法提交,因为在Control里面无法得到Reply类*@
@using (@Html.BeginForm())
{
MvcForum.Models.Reply reply = new MvcForum.Models.Reply();
@Html.DisplayFor(model => model.TopicID);
<textarea class="ckeditor" id="editor1" name="editor1" cols="100" rows="10">@Html.DisplayFor(model => model.Subject)</textarea>
<input type="submit" value="提交回复" />
}问题:
1、这里我想问一下,我这样做对不对?
2、@using (@Html.BeginForm()){}这个里面是提交表格,在下面代码中CreateReply的参数是根据什么决定的,为什么只有一个强类型(创建User)时候,这里可以通过 ActionResult CreateUser(User user)来获取到 user,这这里不行
[HttpPost]
public ActionResult CreateReply(Reply reply)
{
//db.Reply.AddObject(reply);
//db.SaveChanges();
return View();
}不吃饭,坐等
想设计一个话题回复功能,在这涉及到两个表Topic 和Reply,其中Reply中有一个字段为TopicID我想在页面中实现类似 CSDN ,上面是Topic内容,下面是Reply内容,最下方是一个输入框,输入的内容提交保存到Reply里面
这个页面的强类型用的是 Topic(因为要在上面进行显示)这里把View代码贴出来
@model MvcForum.Models.Topic
@{
ViewBag.Title = "Details";
}
<style type="text/css">
hr
{
border: 0;
background-color: #F0F0F0;
height: 1px;
}
</style><script src="../../Scripts/ckeditor/ckeditor.js" type="text/javascript"></script>
<h2>
话题</h2>
@*以下内容显示Topic内容*@
@Html.ActionLink("回复","Reply")
<fieldset style="padding: 10px 0px 10px 0px">
<legend>@Html.DisplayFor(model => model.Subject)</legend>
<br />
<div style="float: left;">
楼主</div>
<div style="border-width: 1px; text-align: right;">
发表于: @Html.DisplayFor(model => model.PubTime)
</div>
<hr />
<div style="margin: 20px 20px 20px 20px;">
@Html.DisplayFor(model => model.Details)
</div>
<hr />
<div style="text-align: right">
引用|管理|对我有用[0]|丢个板砖[0]|top</div>
</fieldset>@*以下内容显示Reply内容*@
@{int i = 1;
foreach (var item in Model.Reply.OrderBy(m=>m.ReplyTime)) {
<br />
<div style="float: left;">@Html.Label(i.ToString())楼</div>
<div style="border-width: 1px; text-align: right;">
回复于: @Html.Label(item.ReplyTime.ToString())
</div>
<hr />
<div>@Html.Label(item.ReplyContent)</div>
<hr />
<div style="text-align: right">
引用|管理|对我有用[0]|丢个板砖[0]|top</div>
i++;
}}
@*通过一个按钮做提交回复,不过这里无法提交,因为在Control里面无法得到Reply类*@
@using (@Html.BeginForm())
{
MvcForum.Models.Reply reply = new MvcForum.Models.Reply();
@Html.DisplayFor(model => model.TopicID);
<textarea class="ckeditor" id="editor1" name="editor1" cols="100" rows="10">@Html.DisplayFor(model => model.Subject)</textarea>
<input type="submit" value="提交回复" />
}问题:
1、这里我想问一下,我这样做对不对?
2、@using (@Html.BeginForm()){}这个里面是提交表格,在下面代码中CreateReply的参数是根据什么决定的,为什么只有一个强类型(创建User)时候,这里可以通过 ActionResult CreateUser(User user)来获取到 user,这这里不行
[HttpPost]
public ActionResult CreateReply(Reply reply)
{
//db.Reply.AddObject(reply);
//db.SaveChanges();
return View();
}不吃饭,坐等
MvcForum.Models.Reply reply = new MvcForum.Models.Reply();
@Html.DisplayFor(model => model.TopicID);
<textarea class="ckeditor" id="editor1" name="editor1" cols="100" rows="10">@Html.DisplayFor(model => model.Subject)</textarea>
<input type="submit" value="提交回复" />
这里面好像写的有问题,我建一个例子研究一下看看
[httppost]
public ActionResult Reply(int id)
你最好看看www.asp.net 中MVC的MvcMusicStore例子
我看了,MvcMusicStore 看了
MasterDetail 也看了,现在就是有这点疑问,如果是在webForm中很好处理,可这里却不知道从哪下手
萝卜,你的 name=名 在哪?
重点是这个,我不知道 control里面从哪得到的参数
@Html.ActionLink("回复","Reply",new {name=model.Name})
你说的那种不是 通过 ActionLink连接吗?现在是通过@using (@Html.BeginForm()){} 然后里面用一个 input type= "submit" 提交来传递到 control默认的情况下,control的参数,是在哪声明?从哪传进去的呢?
好像将所有填写内容收集
一同传给reply(replyModel model)
看看下面的例子:
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Account Information</legend> <div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div> <div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div> <div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div> <p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
controller中 [HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
MigrateShoppingCart(model.UserName);
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
} // If we got this far, something failed, redisplay form
return View(model);
}
将所有的内容放 在 @using 里面是可以的,不过那是针对 一个页面中只有一个强类型,而这里面,我这引用了两个,上面那个是显示TOPIC 下面是 显示Reply 和提交Reply
他们之间有 TopicID这个键关联
@using (@Html.BeginForm())
{
@model MvcForum.Models.Reply
@Html.TextBoxFor(model=>model.Content)
@Html.TextBoxFor(model=>model.Time)
<input type="submit" value="提交回复" />
}这样的方式,来进行添加一个新的 Reply
可是现在 Reply 又有一个 外键 TopicID
这里,不知道该怎么处理了
并且现在这个页面不是@model MvcForum.Models.Reply而是 @model MvcForum.Models.Topic
public ActionResult Create(Topic topic)
{
try
{
// TODO: Add insert logic here
db.Topic.AddObject(topic);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}这里是Topic 的Create Action
Reply 的Action 我该怎么做呢?
{
public Topic top{get;set;}
public Reply rep{get;set;}}
public ActionResult Create(Topic topic)
{
try
{
// TODO: Add insert logic here
db.Topic.AddObject(topic);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
这个Create 里面的参数 Topic topic 是从哪来的,为什么会有值呢??为什么我自己改成 Reply reply 会变成null呢??
<%Html.BeginForm("Report", "Admin",new{id=1}, FormMethod.Post);%>
<!-- -->
[HttpPost]
[SessionFilterAttribute(SessionFilterName.Admin)]
public ActionResult Report(int id)
{
return null;
}
lovesheng1212 这样传了一个参数 ID 进去,要是传递 一个类可以嘛?并且这个类是在 beginForm里面才有了值,而不是有默认初始值
public ViewResult Index()
{
return View(db.Topic.ToList());
}
这里定义了一个类
public class TopicAndReply
{
public static Topic topic { get; set; }
public static Reply reply { get; set; }
}
可是这个类,这样修改了之后,那我应该怎么修改 Index这个 Action呢?
=》
TopicAndReply tar = new TopicAndReply();
tar.topic = db.Topic.ToList();
tar.reply = xxx;
return View(tar);看页面需要,可能页面只需要 topic 那你保持原有写法就可以
@model MvcForum.Models.TopicAndReply可定义之后,在Index Action要怎么处理呢?原先是 Return View(db.Topic.ToList());
public static Reply reply { get; set; }不要用 static
这样是不行的吧, public class TopicAndReply
{
public Topic topic { get; set; }
public Reply reply { get; set; }
}难道要定义成 List<Topic> topic 吗?
可是这样的话,TopicAndReply里面就不是 单纯的强类型了
{
public Topic top{get;set;}
public IList<Reply> repList{get;set;}
}之前的不对。
如果是删除操作,这部分应该怎么改写啊?请教