【发布时间】:2014-09-30 04:53:02
【问题描述】:
我刚开始使用来自 WebForms 的 Asp.Net MVC。作为这种编程模式 (MVC) 的初学者,这让我大吃一惊。我发现很难处理这种编程模式的复杂性。我现在的任务是从视图中的嵌套循环表中捕获输入框的值,以便我可以在控制器的 actionresult 方法中分配它。我需要在嵌套循环表中捕获输入框的值的原因是因为在我的操作方法中,我需要使用带有 where 子句的 linq-sql 进行数据绑定或查询,并将捕获的输入框值的值分配给 where 子句。这就是为什么我需要从 webforms 的角度找到“findcontrol”命令的替代方法来捕获控件的值。因此,如果这里有人熟悉在 webforms 中使用 findcontrol 命令处理 Gridview,那么这就是我正在寻找的替代方法。如何在 Asp.net MVC 中执行此操作?到目前为止,当我打开这个视图时,我遇到了一个运行时错误: 参数字典包含“MyFirstMVCApp.Controllers.ProfileController”中方法“System.Web.Mvc.ActionResult PostComment(Int32)”的不可为空类型“System.Int32”的参数“comid”的空条目'。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数
查看:
@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;
}
【问题讨论】:
-
表格的每一行都包含
<input name="comid"...>。你希望得到哪一个? -
由于控制器中的
comid参数不能为空,因此需要在打开视图时在url中包含comid的值,即/Profile/PostComment/1 -
@StephenMuecke 我需要在所有行中捕获所有不同的输入框值,以便在我的操作方法控制器中进行数据绑定。这在 MVC 中可行吗?因为我已经通过在网格中使用 findcontrol 命令在网络表单中完成了。
-
@ekad 我如何在我的控制器中实现它?
-
@timmack,一旦你了解了 MVC 的基础知识,这就是你要说的关于 Web 表单的内容。
标签: c# asp.net-mvc asp.net-mvc-4 linq-to-sql webforms