最近学习 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();
        }不吃饭,坐等

解决方案 »

  1.   

    这样做可以,不过
    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="提交回复"  />
    这里面好像写的有问题,我建一个例子研究一下看看
      

  2.   

    cotroller中要写一个
    [httppost]
    public ActionResult Reply(int id)
    你最好看看www.asp.net 中MVC的MvcMusicStore例子
      

  3.   

    回复你的问题: 在下面代码中CreateReply的参数是根据什么决定的根据name=名和你的对象里面的属性名相当来匹配的.
      

  4.   


    我看了,MvcMusicStore 看了
    MasterDetail 也看了,现在就是有这点疑问,如果是在webForm中很好处理,可这里却不知道从哪下手 
      

  5.   


    萝卜,你的  name=名  在哪?
    重点是这个,我不知道  control里面从哪得到的参数 
      

  6.   

    @Html.ActionLink("回复","Reply")中要传参数
    @Html.ActionLink("回复","Reply",new {name=model.Name})
      

  7.   

    wade618
    你说的那种不是 通过 ActionLink连接吗?现在是通过@using (@Html.BeginForm()){} 然后里面用一个 input type= "submit"  提交来传递到 control默认的情况下,control的参数,是在哪声明?从哪传进去的呢?
      

  8.   

    @using (@Html.BeginForm()){}
    好像将所有填写内容收集
    一同传给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);
            }
      

  9.   


    将所有的内容放 在  @using 里面是可以的,不过那是针对 一个页面中只有一个强类型,而这里面,我这引用了两个,上面那个是显示TOPIC 下面是 显示Reply 和提交Reply
      

  10.   

    @using (@Html.BeginForm()) 这个里边写上控制器和action的名字,你在看下
      

  11.   

    把[HttpPost]换成[AcceptVerbs(HttpVerbs.Post)]
      

  12.   

    阿非页面中有两个强类型 Topic和Reply  属于主从表关系,我想在 MVC 实现提交回复这个功能
    他们之间有  TopicID这个键关联 
      

  13.   

    你可以做的跟winform一样,request.from["name"] 这么接收值啊
      

  14.   

    如果单独一个 Reply类,那么我可以通过
    @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
      

  15.   

     [HttpPost]
            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 我该怎么做呢?
      

  16.   

    应该使用 ViewModel因为好比你这篇帖子吧我们先叫它 Post 或者 Article,它是由一个Topic和若干个Reply 组成的那么应该是这样public class Post
    {
       public Topic top{get;set;}
       public Reply rep{get;set;}}
      

  17.   

    @model MvcForum.Models.Topic应该是@model MvcForum.Models.Post绑定部分foreach (var item in Model.Reply.OrderBy(m=>m.ReplyTime)) =>foreach (var item in Model.rep.Reply.OrderBy(m=>m.ReplyTime)) 
      

  18.   

    阿非这样好像相当于,一个页面 拥有了多个 Model  我先处理一下看看,另外[HttpPost]
            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呢??
      

  19.   

    是你post过来的数据会执行类似UpdateModel的方法 进行数据绑定工作。关键词 ModelBinder、UpdateModel
      

  20.   

     
    <%Html.BeginForm("Report", "Admin",new{id=1}, FormMethod.Post);%>
    <!--                                                                                           -->
     [HttpPost]
            [SessionFilterAttribute(SessionFilterName.Admin)]
            public ActionResult Report(int id)
            {
                return null;
            }
            
      

  21.   


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

  22.   

    啊非,还在吗之前的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呢?
      

  23.   

    return View(db.Topic.ToList());
    =》
    TopicAndReply tar = new TopicAndReply();
    tar.topic = db.Topic.ToList();
    tar.reply = xxx;
    return View(tar);看页面需要,可能页面只需要 topic  那你保持原有写法就可以
      

  24.   

    是的,之前这个页面是@model MvcForum.Models.Topic而现在我把这个页面强类型 定义为
    @model MvcForum.Models.TopicAndReply可定义之后,在Index  Action要怎么处理呢?原先是 Return View(db.Topic.ToList());
      

  25.   

     public static Topic topic { get; set; }
            public static Reply reply { get; set; }不要用 static 
      

  26.   

    tar.topic = db.Topic.ToList();
    这样是不行的吧, public class TopicAndReply
        {
            public Topic topic { get; set; }
            public Reply reply { get; set; }
        }难道要定义成 List<Topic> topic 吗?
    可是这样的话,TopicAndReply里面就不是 单纯的强类型了 
      

  27.   

    tar.topic = db.Topic.ToList();  无法将类型 List<Topic>转换为类型 Topic
      

  28.   

    你确定你之前的页面是 @model MvcForum.Models.Topic不是 @model IList<MvcForum.Models.Topic>  之类的?
      

  29.   

    Index 这个页显示的应该只是 title , postTime ,author 等和 Topic有关的但返回的应该是 IList<Topic> 这样,你延续你之前的写法就行。
      

  30.   

    而帖子的正文页,应该是由 一个 Topic 和若干个 Reply 组成那ViewModel 应该定义成public class Post
    {
      public Topic top{get;set;}
      public IList<Reply> repList{get;set;}
    }之前的不对。
      

  31.   

    求赐教,[HttpPost]         public ActionResult Create(Topic topic)         {             try            {                 // TODO: Add insert logic here                 db.Topic.AddObject(topic);                 db.SaveChanges();                 return RedirectToAction("Index");             }             catch            {                 return View();             }         }
    如果是删除操作,这部分应该怎么改写啊?请教