节省了多个记录提交点击进入在MVC4实体不同的充。没有得到从视图中值控制器 [英] Saving multiple records on submit click into differnt entities in MVC4. Not getting values from view in Controller
问题描述
我想保存多个学生提交按钮的点击上课。我能够在关注表以创建空白记录,然后针对填充数据。
我有以下视图模型:
公共类TeacherAttendanceModel
{
#区域必需属性 众长ScholarAttendanceId {搞定;组; }
公共字符串学生{搞定;组; }
公共BOOL缺席{搞定;组; }
公共字符串AbsentComment {搞定;组; }
公共BOOL统一{搞定;组; }
公共BOOL作业{搞定;组; }
公共字符串HomeworkComment {搞定;组; }
公共字符串UniformCommentSelected {搞定;组; }
公共IEnumerable的<串GT; UniformComment {搞定;组; } #endregion}
我的控制器是如下图所示。
公共类TeacherAttendanceController:控制器
{
//
// GET:/ TeacherAttendance / 公众的ActionResult指数()
{
长的classId = Success.Business.Roles.Teacher.GetHomeRoomClassID(Convert.ToInt64(会话[GlobalVar.LOGGED_IN_ID]));
VAR classAttendanceStatus = Success.Business.Entities.ClassAttendance.GetClassAttendanceStatus(的classId);
ViewBag.status = classAttendanceStatus;
VAR attendanceData = TeacherAttendance.CreateClassAttendance(的classId);
返回查看(attendanceData);
} [HttpPost]
公众的ActionResult指数(IEnumerable的< TeacherAttendanceModel> teacherAttendanceModel)
{
尝试
{
如果(ModelState.IsValid)
{
TeacherAttendance.SaveAttendance(teacherAttendanceModel);
}
}
赶上(例外五)
{ } 返回查看(teacherAttendanceModel);
}}
获取指数是工作的罚款。但我没有得到中邮指数TeacheAttendanceModel对象。我得到空对象。我会感谢充分获得在这个方面的任何帮助。如何更新考勤上提交多条记录点击?
我使用以下几种观点:
@foreach(以型号VAR项){
&所述; TR>
< TD风格=BORDER-风格:固体;边框颜色:深青灰;边框宽度:薄;>
@ Html.DisplayFor(modelItem => item.Student)
< / TD>
< TD风格=BORDER-风格:固体;边框颜色:深青灰;边框宽度:薄;>
@ Html.CheckBoxFor(modelItem => item.Absent,ViewBag.status == 2新{禁用=禁用}:空)
@ Html.TextBoxFor(modelItem => item.AbsentComment,ViewBag.status == 2新{禁用=禁用}:空)
< / TD>
< TD风格=BORDER-风格:固体;边框颜色:深青灰;边框宽度:薄;>
@ Html.CheckBoxFor(modelItem => item.Uniform,ViewBag.status == 2新{禁用=禁用}:空)
@ Html.DropDownListFor(modelItem => item.UniformCommentSelected,新的SelectList(item.UniformComment),item.UniformCommentSelected ??---选择---,ViewBag.status == 2新{禁用=禁用? } : 空值)
< / TD>
< TD风格=BORDER-风格:固体;边框颜色:深青灰;边框宽度:薄;>
@ Html.CheckBoxFor(modelItem => item.Homework,ViewBag.status == 2新{禁用=禁用}:空)
@ Html.TextBoxFor(modelItem => item.HomeworkComment,ViewBag.status == 2新{禁用=禁用}:空)
< / TD> < / TR>
}
模型:
公共类测试
{
公开名单<串GT; UniformComment {搞定;组; }
}
控制器:
公众的ActionResult指数()
{
VAR模型=新测试
{
UniformComment =新的List<串GT; {一,二,三化}
}; 返回查看(模型);
} [HttpPost]
公众的ActionResult指数(测试模式)
{
返回查看(模型);
}
查看:
@using(Html.BeginForm())
{
对于(VAR I = 0; I< Model.UniformComment.Count;我++)
{
@ Html.TextBoxFor(X => Model.UniformComment [I])
} <输入类型=提交值=保存/>
}
呈现的HTML例如:
<输入ID =UniformComment_0_NAME =UniformComment [0]类型=文本值=一/>
<输入ID =UniformComment_1_NAME =UniformComment [1]类型=文本值=两化/>
<输入ID =UniformComment_2_NAME =UniformComment [2]类型=文本值=三/>
的想法是迭代与为
循环或创建 EditorTemplate
然后您会收到索引项目。
添加(感觉不一样):
查看:
@using(Html.BeginForm())
{
的foreach(在Model.UniformComment VAR评论)
{
@ Html.TextBoxFor(X =>注释)
} <输入类型=提交值=保存/>
}
呈现的HTML:
<输入ID =评论NAME =注释类型=文本值=一/>
<输入ID =评论NAME =注释类型=文本值=两化/>
<输入ID =评论NAME =注释类型=文本值=三/>
I am trying to save the class attendance for multiple students on click of submit button. I am able to create the blank records in the concern tables and then populate the data in view.
I have the following view model:
public class TeacherAttendanceModel
{
#region Required Properties
public long ScholarAttendanceId { get; set; }
public string Student { get; set; }
public bool Absent { get; set; }
public string AbsentComment { get; set; }
public bool Uniform { get; set; }
public bool Homework { get; set; }
public string HomeworkComment { get; set; }
public String UniformCommentSelected { get; set; }
public IEnumerable<String> UniformComment { get; set; }
#endregion
}
My Controller is as below.
public class TeacherAttendanceController : Controller
{
//
// GET: /TeacherAttendance/
public ActionResult Index()
{
long classId = Success.Business.Roles.Teacher.GetHomeRoomClassID(Convert.ToInt64(Session[GlobalVar.LOGGED_IN_ID]));
var classAttendanceStatus = Success.Business.Entities.ClassAttendance.GetClassAttendanceStatus(classId);
ViewBag.status = classAttendanceStatus;
var attendanceData = TeacherAttendance.CreateClassAttendance(classId);
return View(attendanceData);
}
[HttpPost]
public ActionResult Index(IEnumerable<TeacherAttendanceModel> teacherAttendanceModel)
{
try
{
if (ModelState.IsValid)
{
TeacherAttendance.SaveAttendance(teacherAttendanceModel);
}
}
catch (Exception e)
{
}
return View(teacherAttendanceModel);
}
}
Get Index is working fine. But I am not getting the TeacheAttendanceModel object in Post index. I get null object. I would be thank full to get any help in this regards. How to update the multiple records of attendance on submit click?
I am using the following View:
@foreach (var item in Model) {
<tr >
<td style="border-style:solid; border-color:darkslategray; border-width:thin;">
@Html.DisplayFor(modelItem => item.Student)
</td>
<td style="border-style:solid; border-color:darkslategray; border-width:thin;">
@Html.CheckBoxFor(modelItem => item.Absent, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
@Html.TextBoxFor(modelItem => item.AbsentComment, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
</td>
<td style="border-style:solid; border-color:darkslategray; border-width:thin;">
@Html.CheckBoxFor(modelItem => item.Uniform, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
@Html.DropDownListFor(modelItem => item.UniformCommentSelected, new SelectList(item.UniformComment),item.UniformCommentSelected ?? "---Select---", ViewBag.status == 2? new {disabled = "disabled"} : null)
</td>
<td style="border-style:solid; border-color:darkslategray; border-width:thin;">
@Html.CheckBoxFor(modelItem => item.Homework, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
@Html.TextBoxFor(modelItem => item.HomeworkComment, ViewBag.status == 2? new {disabled = "disabled"} : null)
</td>
</tr>
}
Model:
public class Test
{
public List<string> UniformComment { get; set; }
}
Controller:
public ActionResult Index()
{
var model = new Test
{
UniformComment = new List<string>{ "one", "two", "three" }
};
return View(model);
}
[HttpPost]
public ActionResult Index(Test model)
{
return View(model);
}
View:
@using (Html.BeginForm())
{
for (var i = 0; i < Model.UniformComment.Count; i++)
{
@Html.TextBoxFor(x => Model.UniformComment[i])
}
<input type="submit" value="Save" />
}
Rendered html example:
<input id="UniformComment_0_" name="UniformComment[0]" type="text" value="one" />
<input id="UniformComment_1_" name="UniformComment[1]" type="text" value="two" />
<input id="UniformComment_2_" name="UniformComment[2]" type="text" value="three" />
The idea is iterate with for
loop or create EditorTemplate
and then you receive indexed items.
Added (Feel the difference):
View:
@using (Html.BeginForm())
{
foreach (var comment in Model.UniformComment)
{
@Html.TextBoxFor(x => comment)
}
<input type="submit" value="Save" />
}
Rendered html:
<input id="comment" name="comment" type="text" value="one" />
<input id="comment" name="comment" type="text" value="two" />
<input id="comment" name="comment" type="text" value="three" />
这篇关于节省了多个记录提交点击进入在MVC4实体不同的充。没有得到从视图中值控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!