如何将网格记录保存到数据库中 [英] how to save the records of a grid into a database
问题描述
在我的应用程序中,我有Html表格。 HTMl表格在部分视图中可用,在该网格中我有5行,下拉选择以更改Groupmembers。主视图中提供了保存按钮。当用户更改Groupmembers并单击保存按钮时,我想将记录保存到数据库中。如何获取partialview HTML表格网格记录的记录以及如何保存它。而且我想知道,如果记录在数据库中可用,它应该得到更新,否则它应该插入。以下是我的代码:
Index.cshtml:
@model DM_Tool_MVC_Site.ViewModels.GroupHeadViewModels
@ {
ViewBag.Title =Group Heads;
}
< script type =text / javascript>
$(document).ready(function(){
var DivisId = $( '#SelectedDivision选项:选中')。val();
if(DivisId == 0){
$(#btnEdit)。hide(); < br $>
$(#btnUpdate)。hide();
}
else {
$(# btnEdit)。show();
}
// $(#btnEdit)。hide();
// $ (#btnUpdate)。hide();
// $('#GroupList')。hide();
$('#SelectedDivision')。更改(function(){
var DivId = $('#SelectedDivision option:selected')。val();
if(DivId == 0){
$(#btnEdit)。hide();
$(#btnUpdate)。hide();
}
else
{
$(#btnEdit)。show();
$(#btnUpdate)。hide(); < br $>
}
$ .get('@ Url.Action(GroupHeadIndexPartial)',
{DivisionID:DivId,EditMode:false}, function(data){
// $('#GroupList')。show();
$(#GroupList)。html(data);
});
});
});
< / script>
用户管理
@using(Html.BeginForm(Index,GroupHead,FormMethod.Post,new {@class =form-horizontal,role = form}))
{
@ Html.AntiForgeryToken()
@ ViewBag.Title
Hi,
In my application I am having Html table grid. The HTMl table grid is available in the partialview, in that grid I am having 5 rows with the dropdownselection to change the Groupmembers. The save button is available in the Main view. When the user changes the Groupmembers and click on save button I want to save the records into the database. How to get the records of the partialview HTML table grid records and how to save it. And also I want to know, if the records is available in database it should get Update else it should insert. Below is my code:
Index.cshtml:
@model DM_Tool_MVC_Site.ViewModels.GroupHeadViewModels
@{
ViewBag.Title = "Group Heads";
}
<script type="text/javascript">
$(document).ready(function () {
var DivisId = $('#SelectedDivision option:selected').val();
if (DivisId == 0) {
$("#btnEdit").hide();
$("#btnUpdate").hide();
}
else {
$("#btnEdit").show();
}
//$("#btnEdit").hide();
//$("#btnUpdate").hide();
//$('#GroupList').hide();
$('#SelectedDivision').change(function () {
var DivId = $('#SelectedDivision option:selected').val();
if (DivId == 0) {
$("#btnEdit").hide();
$("#btnUpdate").hide();
}
else
{
$("#btnEdit").show();
$("#btnUpdate").hide();
}
$.get('@Url.Action("GroupHeadIndexPartial")',
{ DivisionID: DivId,EditMode:false }, function (data) {
//$('#GroupList').show();
$("#GroupList").html(data);
});
});
});
</script>
User Management
@using (Html.BeginForm("Index", "GroupHead", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@ViewBag.Title
@ Html.LabelFor(m => m.SelectedDivision,new { @class =col-md-2 control-label})
@Html.LabelFor(m => m.SelectedDivision, new { @class = "col-md-2 control-label" })
@ Html.DropDownListFor(m = > m.SelectedDivision,Model.GetDivision()。选择(u => new SelectListItem
{
Text = u.Text,
Value = u.Value,
Selected = u.Text == ViewBag.CurrentSelection as string
}), - 选择分区 - ,新{@ class =form-control})
@Html.DropDownListFor(m => m.SelectedDivision, Model.GetDivision().Select(u => new SelectListItem
{
Text = u.Text,
Value = u.Value,
Selected = u.Text == ViewBag.CurrentSelection as string
}), "--Choose Division--", new { @class = "form-control" })
&l t;输入类型=提交名称=编辑值=编辑类=btn btn-defaultid =btnEdit/>
<input type="submit" name="Edit" value="Edit" class="btn btn-default" id="btnEdit" />
@ Html.Action(GroupHeadIndexPartial,new {DivisionID = ViewBag.CurrentSelection as string,EditMode = ViewBag。 EditMode as string})
@Html.Action("GroupHeadIndexPartial", new { DivisionID = ViewBag.CurrentSelection as string, EditMode = ViewBag.EditMode as string })
@if(Model.IsEditMode == false)
{
< input type =submitname =Updateclass =btn btn-defaultvalue =更新id =btnUpdate禁用=禁用/>
}
其他
{
< input type =submitname =Updateclass =btn btn-defaultvalue =Updateid =btnUpdate/>
}
@if (Model.IsEditMode == false)
{
<input type="submit" name="Update" class="btn btn-default" value="Update" id="btnUpdate" disabled="disabled" />
}
else
{
<input type="submit" name="Update" class="btn btn-default" value="Update" id="btnUpdate" />
}
}
@section脚本{
@ Scripts.Render( 〜/ bundles / jqueryval)
}
GroupIndexPartialView.cshtml:
@model IEnumerable< dm_tool_mvc_site.viewmodels.groupheadlist>
@foreach(型号中的var项目)
{
}
团队 | 团长$> |
---|---|
@ Html.DisplayFor(modelItem => item.GroupName) | @ Html.HiddenFor(modelItem => item.DivID) @ Html.HiddenFor(modelItem => item .GroupId) @ Html.DropDownListFor(modelItem => item.GroupHeadUserName,item.UsersSelectList(item.DivID,item.GroupId).Select(u => new SelectListItem { Text = u.Text, Value = u.Value, Selected = u.Text == item.GroupHeadUserName }), - 选择组头 - ,item.EditMode?(对象)new {@class =form-control}:new {@readonly =readonly,@ class =form-control}) @ * @ Html.DisplayFor(modelItem => item.GroupHeadUserName)* @ |
谢谢
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
GroupIndexPartialView.cshtml:
@model IEnumerable<dm_tool_mvc_site.viewmodels.groupheadlist>
@foreach (var item in Model)
{
}
Group | Group Head |
---|---|
@Html.DisplayFor(modelItem => item.GroupName) | @Html.HiddenFor(modelItem => item.DivID) @Html.HiddenFor(modelItem => item.GroupId) @Html.DropDownListFor(modelItem => item.GroupHeadUserName, item.UsersSelectList(item.DivID,item.GroupId).Select(u => new SelectListItem { Text = u.Text, Value = u.Value, Selected = u.Text == item.GroupHeadUserName }), "--Select Group Head--", item.EditMode ? (object)new { @class = "form-control" } : new { @readonly = "readonly", @class = "form-control" }) @*@Html.DisplayFor(modelItem => item.GroupHeadUserName)*@ |
Thanks
推荐答案
(文件).ready(function( ){
var DivisId =
(document).ready(function () {
var DivisId =
('#SelectedDivision opti on:选中')。val();
if(DivisId == 0){
('#SelectedDivision option:selected').val();
if (DivisId == 0) {
(#btnEdit)。hide( );
("#btnEdit").hide();
这篇关于如何将网格记录保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!