ASP.NET MVC 使用 Ajax 提交表单,后台接收后值为 null?

sinat_24499499 2016-05-17 01:12:31
@using (Ajax.BeginForm(ajaxopts))
{
@Html.LabelFor(model => model.comment.FileId, htmlAttributes: new { @class = "control-label col-md-2" })
<input class="form-control text-box single-line" data-val="true" data-val-number="The field FileId must be a number." data-val-required="FileId 字段是必需的。" id="FileId" name="FileId" type="number" value="@ViewBag.FileId"; />

@Html.LabelFor(model => model.comment.CreateDate, htmlAttributes: new { @class = "control-label col-md-2" })
<input class="form-control text-box single-line" data-val="true" data-val-date="The field CreateDate must be a date." data-val-required="CreateDate 字段是必需的。" id="CreateDate" name="CreateDate" type="datetime" value="@DateTime.Now.ToString()"; />

@Html.LabelFor(model => model.comment.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
<input class="form-control text-box single-line" id="UserId" name="UserId" type="text" value="@User.Identity.Name.ToString()"; />

@Html.Validati onMessageFor(model => model.comment.UserId, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.comment.review, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.comment.review, new { htmlAttributes = new { @class = "form-control" } })
<input type="submit" value="Create" class="btn btn-default" />
}
</div>
<div id="loading" style="display:none">
<p>正在提交服务器。。。</p>
</div>
http://ww1.sinaimg.cn/mw690/e41b183dgw1f3y6p9w2yzj20iz066wg7.jpg
http://ww4.sinaimg.cn/mw690/e41b183dgw1f3y6pb0t81j20yu0970wg.jpg
http://ww3.sinaimg.cn/mw690/e41b183dgw1f3y6q6lkekj20g008vdgc.jpg
http://ww1.sinaimg.cn/mw690/e41b183dgw1f3y6q5zm4ij20ku031756.jpg
...全文
673 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
BitCoffee 2016-05-19
  • 打赏
  • 举报
回复
你在控制器里面,给实体赋值, public ActionResult Index() { Test model = new Test(); model.User = "user"; return View(model); } View里面: @Html.TextBoxFor(model => model.User, new { @class = "form-control text-box single-line" }) @Html.HiddenFor(model => model.User) 这样就可以了,另外有些隐藏字段是需要在前端获取值得,只要在提交之前通过js,jquery给隐藏的控件的value赋值即可。
sinat_24499499 2016-05-18
  • 打赏
  • 举报
回复
引用 13 楼 BitCoffee 的回复:
@Html.TextBoxFor(model => model.comment.FileId, new { @class = "form-control text-box single-line", @Value = model.comment.FileId }) 默认值@Value,这里的V要大写。 或者你用jquery去赋值。
我代码是这样写的 @Html.HiddenFor(model => model.comment.CreateDate, new { htmlAttributes = new { @class = "form-control", @Value = @DateTime.Now.ToString() } }) 生成的HTML 代码是这样的,明显value 为空 <input data-val="true" data-val-date="字段 CreateDate 必须是日期。" data-val-required="CreateDate 字段是必需的。" htmlAttributes="{ class = form-control, Value = 2016/5/18 10:12:43 }" id="comment_CreateDate" name="comment.CreateDate" type="hidden" value="" />
sinat_24499499 2016-05-18
  • 打赏
  • 举报
回复
引用 13 楼 BitCoffee 的回复:
@Html.TextBoxFor(model => model.comment.FileId, new { @class = "form-control text-box single-line", @Value = model.comment.FileId }) 默认值@Value,这里的V要大写。 或者你用jquery去赋值。
您好,使用@Html.HiddenFor的时候值没传到后台
BitCoffee 2016-05-18
  • 打赏
  • 举报
回复
@Html.TextBoxFor(model => model.comment.FileId, new { @class = "form-control text-box single-line", @Value = model.comment.FileId }) 默认值@Value,这里的V要大写。 或者你用jquery去赋值。
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
引用 11 楼 BitCoffee 的回复:
@type = "number", @value = "@ViewBag.FileId 这些一般不需要,默认是根据model.comment.FileId这个字段属性来生成标签的。
那怎么给他们定义默认值
BitCoffee 2016-05-17
  • 打赏
  • 举报
回复
@type = "number", @value = "@ViewBag.FileId 这些一般不需要,默认是根据model.comment.FileId这个字段属性来生成标签的。
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
引用 8 楼 BitCoffee 的回复:
labelfor生成的标签是label,提交时不会post这个数据
<div class="form-group"> @Html.LabelFor(model => model.comment.FileId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextBoxFor(model => model.comment.FileId, new { @class = "form-control text-box single-line", @type = "number", @value = "@ViewBag.FileId" }) 貌似这样也不行啊
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
引用 8 楼 BitCoffee 的回复:
labelfor生成的标签是label,提交时不会post这个数据
是吗,我试试看,先行谢过
BitCoffee 2016-05-17
  • 打赏
  • 举报
回复
labelfor生成的标签是label,提交时不会post这个数据
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
引用 6 楼 BitCoffee 的回复:

input的这些控件需要换成 @Html.TextBoxFor
有些字段不需要在前端页面显示给用户,但是又得传到后台控制器的,在前端用@Html.HiddenFor
不可以用labelfor吗?
BitCoffee 2016-05-17
  • 打赏
  • 举报
回复

input的这些控件需要换成 @Html.TextBoxFor
有些字段不需要在前端页面显示给用户,但是又得传到后台控制器的,在前端用@Html.HiddenFor
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
引用 4 楼 u010941149 的回复:
userid没传过来
除了review ,其他的都没传过来
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复
sdfgrtyu 2016-05-17
  • 打赏
  • 举报
回复
userid没传过来
sinat_24499499 2016-05-17
  • 打赏
  • 举报
回复

62,270

社区成员

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

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

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

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