MVC 中HttpPost提交,在Control里面的参数是怎么确定的?

ruanwei1987 2011-11-03 12:06:51
最近学习 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();
}

不吃饭,坐等
...全文
3077 51 打赏 收藏 转发到动态 举报
写回复
用AI写文章
51 条回复
切换为时间正序
请发表友善的回复…
发表回复
wilsonmylove 2013-06-01
  • 打赏
  • 举报
回复
求赐教,[HttpPost] public ActionResult Create(Topic topic) { try { // TODO: Add insert logic here db.Topic.AddObject(topic); db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }


如果是删除操作,这部分应该怎么改写啊?请教
shuoaying 2013-04-24
  • 打赏
  • 举报
回复
最终结果是什么啊???同求。
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
恩,我那不知道怎么写,随便写看结果呢,谢谢了
幸福的小木鱼 2011-11-03
  • 打赏
  • 举报
回复
这样做可以,不过
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="提交回复" />
这里面好像写的有问题,我建一个例子研究一下看看
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复


真的在等
水猿兵团五哥 2011-11-03
  • 打赏
  • 举报
回复
真的等啊?我不明白在问什么
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
阿非,我再 开个帖子,你回复下吧
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
[Quote=引用 45 楼 sandy945 的回复:]

而帖子的正文页,应该是由 一个 Topic 和若干个 Reply 组成

那ViewModel 应该定义成

public class Post
{
public Topic top{get;set;}
public IList<Reply> repList{get;set;}
}

之前的不对。
[/Quote]
恩,
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
我搞迷了,我再继续学习,

谢谢啊非
阿非 2011-11-03
  • 打赏
  • 举报
回复
而帖子的正文页,应该是由 一个 Topic 和若干个 Reply 组成

那ViewModel 应该定义成

public class Post
{
public Topic top{get;set;}
public IList<Reply> repList{get;set;}
}

之前的不对。
阿非 2011-11-03
  • 打赏
  • 举报
回复
Index 这个页显示的应该只是 title , postTime ,author 等和 Topic有关的

但返回的应该是 IList<Topic> 这样,你延续你之前的写法就行。
阿非 2011-11-03
  • 打赏
  • 举报
回复
你确定你之前的页面是 @model MvcForum.Models.Topic

不是 @model IList<MvcForum.Models.Topic> 之类的?
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
tar.topic = db.Topic.ToList(); 无法将类型 List<Topic>转换为类型 Topic
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
tar.topic = db.Topic.ToList();


这样是不行的吧,

public class TopicAndReply
{
public Topic topic { get; set; }
public Reply reply { get; set; }
}

难道要定义成 List<Topic> topic 吗?
可是这样的话,TopicAndReply里面就不是 单纯的强类型了
阿非 2011-11-03
  • 打赏
  • 举报
回复
public static Topic topic { get; set; }
public static Reply reply { get; set; }

不要用 static
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
是的,之前这个页面是

@model MvcForum.Models.Topic

而现在我把这个页面强类型 定义为
@model MvcForum.Models.TopicAndReply

可定义之后,在Index Action要怎么处理呢?

原先是 Return View(db.Topic.ToList());
阿非 2011-11-03
  • 打赏
  • 举报
回复
return View(db.Topic.ToList());
=》
TopicAndReply tar = new TopicAndReply();
tar.topic = db.Topic.ToList();
tar.reply = xxx;
return View(tar);

看页面需要,可能页面只需要 topic 那你保持原有写法就可以
阿非 2011-11-03
  • 打赏
  • 举报
回复
有疑问?
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
啊非,还在吗

之前的Index Action是这样的


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呢?
ruanwei1987 2011-11-03
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 lovesheng1212 的回复:]

HTML code

<%Html.BeginForm("Report", "Admin",new{id=1}, FormMethod.Post);%>
<!-- -->
[HttpPost]
[……
[/Quote]

lovesheng1212 这样传了一个参数 ID 进去,要是传递 一个类可以嘛?并且这个类是在 beginForm里面才有了值,而不是有默认初始值
加载更多回复(29)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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