asp.net mvc中webforms的findcontrol命令的替代方法是什么?

时间:2014-09-30 04:53:02

标签: c# asp.net-mvc asp.net-mvc-4 linq-to-sql webforms

我刚开始使用来自WebForms的Asp.Net MVC。作为这种编程模式(MVC)的初学者,它让我的思绪大放异彩。我发现很难处理这种编程模式的复杂性。我现在的任务是从视图中的嵌套循环表中捕获输入框的值,以便我可以在我的控制器的actionresult方法中分配它。我之所以需要捕获嵌套循环表中输入框的值,是因为在我的操作方法中我需要使用带有where子句的linq-sql进行数据绑定或查询,并将捕获的输入框值的值分配给where子句。这就是为什么我需要从webforms透视图中找到“findcontrol”命令的替代方法来捕获控件的值。所以,如果这里有人熟悉使用webforms中的findcontrol命令处理Gridview,那么这就是我正在寻找的替代方案。如何在Asp.net MVC中执行此操作?到目前为止,当我打开这个视图时,我遇到了运行时错误: 参数字典包含非可空类型'System.Int32'参数'comid'的空条目,用于'MyFirstMVCApp.Controllers.ProfileController中方法'System.Web.Mvc.ActionResult PostComment(Int32)' ”。可选参数必须是引用类型,可空类型,或者声明为可选参数。 参数名称:参数

查看:

    @using (Html.BeginForm("PostComment", "Profile", FormMethod.Post, new { }))
{   
      <table>     
@foreach (var item in Model.Comments )
{
    <tr>
        <td>
        <div class="editor-field" style="display:none; margin-bottom:10px;margin-top:10px">
   // I need to capture the value of this inputbox into my action method controller
         <input type="text" id="comidvalue" name="comid" value="@Html.DisplayFor(modelItem=>item.Id)" />

        </div>

         <div style="font-weight:bold;">  @Html.DisplayFor(modelItem => item.name) </div> 

  <p style ="margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :500px; min-height :5px;  display :block; background-color: #CCCCFF">  @Html.DisplayFor(modelItem => item.comment) </p>
  <p style="margin-top:2px;margin-bottom:0px"> <input type="button" id="like" name="like" value="Like" style="color:blue;border:0px;background-color:inherit;cursor:pointer" /> <input type="button" id="Reply" name="Reply" value="Replie(s)" style="color:blue;border:0px;background-color:inherit;cursor:pointer" /></p>

          <div id="divrep" style="position:relative;left:50px; overflow:auto;margin-top:0px">
             <table>
             @for(int i=0;i<Model.Replies.Count;i++)
              {
                 <tr>
                     @Html.HiddenFor(m=>m.Comments[i].Id)
                     <td>
                     <p style ="margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :445px; min-height :5px;  display :block; background-color: #CCCCFF;">@Html.DisplayFor(m=>m.Replies[i].reply)  </p>
                           <br />


                   </td>
                 </tr>
              } 
            </table>


         </div> 

           <input type="text" id="namerep" name="namerep" />
       <span class="field-validation-valid" data-valmsg-for="namerep" data-valmsg-replace="true"></span>
                         <br />
              <textarea id="reply" name="reply" style="width:500px;height:100px;resize:none" ></textarea>
      <span class="field-validation-valid" data-valmsg-for="reply" data-valmsg-replace="true"></span>

    <br />
           <input type="submit" value="Post Reply" name="butname" />

        </td>


    </tr>

}

</table>


      }

控制器:

// this int comid variable is the one I expect from the captured value of the inputbox
    public ActionResult PostComment(int comid) 
        {

            var vModel = new CreateViewModel();

            vModel.Comments = comrepository.GetAllComments().ToList();

            vModel.Reply = replyrepository.GetReplybyID(comid);


            return View(vModel); 
        }

型号:

 public class CommentModel
    {
        public int Id { get; set; }

     //   [Required(ErrorMessage="Don't miss to put your name.")]
        public string name { get; set; }

      //  [Required(ErrorMessage = "Don't leave your comments empty.")]
        public string comment { get; set;}


    }

    public class ReplyModel
    {
        public int idrep { get; set; }
        public string namerep { get; set; }
        public string reply { get; set; }
    } 

    public class CreateViewModel
    {
        public CommentModel CreateComment { get; set; } // this line is optional
        public ReplyModel CreateReply { get; set; }
        public List<CommentModel> Comments { get; set; }
        public List<ReplyModel> Replies { get; set; }
        public ReplyModel Reply { get; set; }
    }

存储库:

 public IEnumerable<ReplyModel> GetReplybyID(int Id)
          {
              List<ReplyModel> profiles = new List<ReplyModel>();
              var prof = from profile in Reprepository.RepTabs
                          where profile.Id == Id
                         orderby profile.Id descending
                         select profile;
              var user = prof.ToList();
              foreach (var item in user)
              {
                  profiles.Add(new ReplyModel()
                  {
                      idrep = item.Id,
                      namerep = item.Name,
                      reply = item.Replies

                  });
              }
              return profiles;
          }

1 个答案:

答案 0 :(得分:1)

当您在控制器获取操作方法时,您无法从视图中获取任何内容,因为视图尚未呈现。您需要在控制器中执行的操作方法是使用模型变量传递视图中所需的所有信息。

看起来您收到错误是因为您使用此网址打开视图:/Profile/PostCommentPostComment获取操作方法需要一个不能为null的整数参数。

根据讨论,您希望显示每条评论的回复,因此Replies属性应位于CommentModel而不是CreateViewModel。将您的模型更改为此

public class CommentModel
{
    public CommentModel()
    {
        this.Replies = new List<ReplyModel>();
    }

    public int Id { get; set; }

 //   [Required(ErrorMessage="Don't miss to put your name.")]
    public string name { get; set; }

  //  [Required(ErrorMessage = "Don't leave your comments empty.")]
    public string comment { get; set;}

    public List<ReplyModel> Replies { get; set; }
}

public class CreateViewModel
{
    public CreateViewModel()
    {
        this.Comments = new List<CommentModel>();
    }

    public CommentModel CreateComment { get; set; } // this line is optional
    public ReplyModel CreateReply { get; set; }
    public List<CommentModel> Comments { get; set; }
    public ReplyModel Reply { get; set; }
}

您需要删除控制器方法中的comid参数,因为它实际上位于模型的Comments属性中。您可以循环浏览vModel.Comments

来获取回复
public ActionResult PostComment()
{
    var vModel = new CreateViewModel();
    vModel.Comments = comrepository.GetAllComments().ToList();

    // loop through vModel.Comments
    for (int i = 0; i < vModel.Comments.Count; i++)
    {
        vModel.Comments[i].Replies = replyrepository.GetReplybyID(vModel.Comments[i].Id).ToList();
    }

    return View(vModel); 
}

在您看来,更改此部分

@for(int i=0;i<Model.Replies.Count;i++)
{
     <tr>
         @Html.HiddenFor(m=>m.Comments[i].Id)
         <td>
         <p style ="margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :445px; min-height :5px;  display :block; background-color: #CCCCFF;">@Html.DisplayFor(m=>m.Replies[i].reply)  </p>
               <br />


       </td>
     </tr>
} 

到这个

@for (int i = 0; i < item.Replies.Count; i++)
{
    <tr>
        @Html.HiddenFor(m => item.Id)
        <td>
        <p style ="margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :445px; min-height :5px;  display :block; background-color: #CCCCFF;">
        @Html.DisplayFor(m=>item.Replies[i].reply)  </p>
        <br />


        </td>
    </tr>
}