C# asp.net mvc中webforms中findcontrol命令的替代方法是什么?
我不熟悉使用来自WebForms的Asp.NETMVC。作为这种编程模式(MVC)的初学者,它让我的思维被打乱了。我发现很难处理这种编程模式的复杂性。我现在的任务是从视图中的嵌套循环表中捕获inputbox的值,以便在控制器的actionresult方法中分配它。之所以需要在嵌套循环表中捕获inputbox的值,是因为在我的操作方法中,我需要使用linq sql with where子句进行数据绑定或查询,并将捕获的inputbox值的值分配给where子句。这就是为什么我需要从webforms透视图中找到“findcontrol”命令的替代方法来捕获控件的值。因此,如果这里有人熟悉在webforms中使用findcontrol命令处理Gridview,这就是我要寻找的替代方法。如何在Asp.net MVC中实现这一点?到目前为止,当我打开此视图时,出现了一个运行时错误: 参数字典包含“MyFirstMVCApp.Controllers.ProfileController”中方法“System.Web.Mvc.ActionResult PostComment(Int32)”的不可空类型“System.Int32”的参数“comid”的空条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 查看:C# asp.net mvc中webforms中findcontrol命令的替代方法是什么?,c#,asp.net-mvc,asp.net-mvc-4,linq-to-sql,webforms,C#,Asp.net Mvc,Asp.net Mvc 4,Linq To Sql,Webforms,我不熟悉使用来自WebForms的Asp.NETMVC。作为这种编程模式(MVC)的初学者,它让我的思维被打乱了。我发现很难处理这种编程模式的复杂性。我现在的任务是从视图中的嵌套循环表中捕获inputbox的值,以便在控制器的actionresult方法中分配它。之所以需要在嵌套循环表中捕获inputbox的值,是因为在我的操作方法中,我需要使用linq sql with where子句进行数据绑定或查询,并将捕获的inputbox值的值分配给where子句。这就是为什么我需要从webforms
@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;
}
型号:
@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;
}
公共类模型
{
公共int Id{get;set;}
//[必需(ErrorMessage=“不要错过输入您的姓名。”)]
公共字符串名称{get;set;}
//[必需(ErrorMessage=“不要将您的评论留空。”)]
公共字符串注释{get;set;}
}
公共类ReplyModel
{
public int idrep{get;set;}
公共字符串namerep{get;set;}
公共字符串回复{get;set;}
}
公共类CreateViewModel
{
public CommentModel CreateComment{get;set;}//此行是可选的
公共ReplyModel CreateReply{get;set;}
公共列表注释{get;set;}
公共列表回复{get;set;}
公共ReplyModel Reply{get;set;}
}
存储库:
@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;
}
public IEnumerable GetReplybyID(int-Id)
{
列表配置文件=新列表();
var prof=来自repreprepository.RepTabs中的配置文件
其中profile.Id==Id
orderby profile.Id降序
选择配置文件;
var user=prof.ToList();
foreach(用户中的var项)
{
profiles.Add(新的ReplyModel()
{
idrep=项目Id,
namerep=item.Name,
回复=项目。回复
});
}
返回配置文件;
}
当您使用controller get action方法时,无法从视图中获取任何内容,因为视图尚未渲染。在controller get action方法中需要做的是使用模型变量传递视图中需要的所有信息
由于使用以下url打开视图,/Profile/PostComment
,而PostComment
获取操作方法需要一个不能为null的整数参数,因此您似乎收到了错误
根据讨论,您希望显示每个评论的回复,因此回复
属性应位于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; }
}
在您看来,更改此部分
@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;im.Comments[i].Id)
@Html.DisplayFor(m=>m.reply[i].reply)
}
对此
@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>
}
@for(int i=0;iitem.Id)
@DisplayFor(m=>item.replays[i].replay)
}
表格的每一行都包含
。您希望得到哪一个?由于控制器中的comid
参数不能为空,因此在打开视图时需要在url中包含comid
值,即/Profile/PostComment/1
@StephenMuecke我需要捕获所有行中的所有不同inputbox值,以便在我的操作方法控制器中进行数据绑定。这在MVC中可行吗?因为我在webforms中是通过在网格中使用findcontrol命令来完成的。@ekad我如何在我的控制器中实现它?@timmack,一旦您了解了MVC的基本知识,这就是关于web窗体的内容。@ekad…这是我使用的第一个代码,没有错误,并且在视图中显示所有注释。然而,我需要的答复是数据索引,以便我显示的答复以及每个评论。现在,没有错误,但是回复没有显示,因为您删除了comid参数,并且代码应该显示回复,这是一个vModel.Reply=replyrepository.GetReplybyID(comid);您能否更新问题并添加replyrepository.GetReplybyID
方法的代码?如果您想显示每个评论的所有回复,我认为应该有一个存储库方法,该方法基于评论id返回多个回复。我不确定为什么replyrepository.GetReplybyID
只返回一个回复。存储库代码中的id
参数根本没有使用,您确定吗?如果是这样,则可以删除该参数,并可以添加vModel.repress=