Jqgrid Master Detail使用ViewModel类提交表单

时间:2012-03-10 04:16:15

标签: c# jquery asp.net-mvc jqgrid

我有一个asp.net MVC View页面,它是Master / Detail风格 从this读取后,我意识到在发送到Controller Layer之前我需要序列化jqgrid数据。

[HttpPost]
public ActionResult Detail(Seminar Seminar, List<Seminar_Detail> Seminar_Details)
{

}

所以我的问题是,
1.如何使用2个ViewModel类将数据发送到控制器    表单输入字段为SeminarMaster,Jqgrid整行为SeminarDetail

2.如果你说,我只有一种方法可以序列化为jqgrid,那么我还需要序列化表单输入字段吗?

3.如果我必须使用序列化方式,我是否有机会使用ViewModel classess将数据解析为控制器层?

更新

JqG​​rid代码或视图层实现

或详细页面,用户可以进行插入/更新或删除过程。

@model MvcMobile.ViewModel.SeminarListDetailViewModel

@{
ViewBag.Title = "Detail";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Detail</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Seminar</legend>
    <table>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Seminar.Seminar_Code)
                </div>
            </td>
            <td>
               <div class="editor-field">
                    @Html.EditorFor(model => model.Seminar.Seminar_Code)
                    @Html.ValidationMessageFor(model => model.Seminar.Seminar_Code)
                </div> 
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Seminar.Title)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Seminar.Title)
                    @Html.ValidationMessageFor(model => model.Seminar.Title)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Seminar.Description)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Seminar.Description)
                    @Html.ValidationMessageFor(model => model.Seminar.Description)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Seminar.Room)
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Seminar.Room)
                    @Html.ValidationMessageFor(model => model.Seminar.Room)
                </div>
            </td>
        </tr>        
    </table>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>


<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
<script type="text/javascript">
jQuery(document).ready(function () {

    var AllSpeakers = (function () {
    var list = null;

    $.ajax({
        'async': false,
        'global': false,
        'url': 'GetAllSpeakers',
        'dataType': 'json',
        'success': function (data) {
            list = data;
        }
    });

        return list;
    })();



    var AllTags = (function () {
        var list = null;

        $.ajax({
            'async': false,
            'global': false,
            'url': 'GetAllTags',
            'dataType': 'json',
            'success': function (data) {
                list = data;
            }
        });

        return list;
    })();


    var lastSel = 0;
    jQuery("#list").jqGrid({
        url: '/Seminar/DetailGridData/',
        editurl: "/Seminar/MyEdit/",
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Seminar_Code', 'Speaker', 'Tag', 'DateAndTime'],
        colModel: [
                      { name: 'Seminar_Code', index: 'Seminar_Code', width: 40, align: 'left', editable: true, edittype: "text", editoptions: { size: "35", maxlength: "50"} },
                      { name: 'SpeakerID', index: 'SpeakerID', align: 'left', editable: true, edittype: "select", formatter: 'select', editoptions: { value: AllSpeakers, 
                                  dataEvents: [
                                                { type: 'change',
                                                    fn: function (e) {
                                                        //alert("Speaker change"); // For Cascading Dropdownlist Or Dependent Combo
                                                    }
                                                }
                                            ]
                                  }, editrules: { required: true}
                            },

                      { name: 'TagID', index: 'TagID', align: 'left', editable: true, edittype: 'select', formatter: 'select', editoptions: { value: AllTags }, editrules: { required: true} },
                      { name: 'DateAndTime', index: 'DateAndTime', width: 40, align: 'left', editable: true, edittype: "text", editoptions: { size: "35", maxlength: "50"} }
                 ],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'SpeakerName',
        sortorder: "desc",
        viewrecords: true,
        autowidth: true,
        autoheight: true,
        imgpath: '/scripts/themes/black-tie/images',
        caption: 'My first grid'
    })
    $("#list").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, refresh: false, search: false });
    $("#list").jqGrid('inlineNav', '#pager', {
        addtext: "Add",
        edittext: "Edit",
        savetext: "Save",
        canceltext: "Cancel",
        addParams: {
            addRowParams: {
                // the parameters of editRow used to edit new row
                keys: true,
                oneditfunc: function (rowid) {
                    alert("new row with rowid=" + rowid + " are added.");
                }
            }
        },
        editParams: {
            // the parameters of editRow
            key: true,
            oneditfunc: function (rowid) {
                alert("row with rowid=" + rowid + " is editing.");
            }
        },
        cancelParams: {
            key: true,
            oneditfunc: function (rowid) {
                //alert("row with rowid=" + rowid + " cancel.");
            }
        }
    });

}); 
</script>

}

<div>
@Html.ActionLink("Back to List", "Index")
</div>
控制器层代码
public class SeminarController : Controller
{
ISeminarListDetailRepository seminarRepository;

//
// Dependency Injection enabled constructors

public SeminarController()
    : this(new  SeminarListDetailRepository()) {
}

public SeminarController(ISeminarListDetailRepository repository)
{
    seminarRepository = repository;
}

/// <summary>
/// Just for Listing page.
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
    return View();
}

/// <summary>
/// Master Detail CRUD form according to previous form's choosen code.
/// </summary>
/// <param name="Seminar_Code"></param>
/// <returns></returns>
public ActionResult DetailByCode(string Seminar_Code)
{
    return View();
}

/// <summary>
/// Master Detail CRUD form.
/// </summary>
/// <returns></returns>
public ActionResult Detail()
{
    var SeminarListDetailViewModel = new SeminarListDetailViewModel();
    return View(SeminarListDetailViewModel);
}

/// <summary>
/// It is this method what I really focus when it comes to save all rows of jqgrid at once.
/// </summary>
/// <param name="Seminar"></param>
/// <param name="Seminar_Details"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Detail(Seminar Seminar, List<Seminar_Detail> Seminar_Details)
{            
    return View();
}       


/// <summary>
/// Just for test purpose only.
/// I will not use this method when I know how to save JQGrid's All Rows at once.
/// </summary>
/// <param name="Seminar_Detail"></param>
/// <returns></returns>
public ActionResult MyEdit(Seminar_Detail Seminar_Detail)
{
    //var seminar_Code = Seminar_Detail.Seminar_Code;
    var jsonData = new
    {

    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

public ActionResult GetAllSpeakers()
{
    string AllSpeakers = " : ;";
    var Speakers = seminarRepository.GetAllSpeakerList().Seminar_Item_Speaker;
    for (int i = 0; i < Speakers.Count; i++)
    {
        if (i == Speakers.Count - 1)
        {
            ///For the last row
            AllSpeakers += Speakers[i].SpeakerID + ":" + Speakers[i].SpeakerName;
        }
        else
        {
            AllSpeakers += Speakers[i].SpeakerID + ":" + Speakers[i].SpeakerName + ";";
        }

    }   
    return Json(AllSpeakers, JsonRequestBehavior.AllowGet);
}

public JsonResult GetAllTags()
{   
    string AllTags = " : ;";
    var Tags = seminarRepository.GetAllTagList().Seminar_Item_Tag;
    for (int i = 0; i < Tags.Count; i++)
    {
        if (i == Tags.Count - 1)
        {
            ///For the last row
            AllTags += Tags[i].TagID + ":" + Tags[i].TagName;
        }
        else
        {
            AllTags += Tags[i].TagID + ":" + Tags[i].TagName + ";";
        }

    }   
    return Json(AllTags, JsonRequestBehavior.AllowGet);
}

public ActionResult DetailGridData(string sidx, string sord, int page, int rows)
{
    int currentPage = Convert.ToInt32(page) - 1;
    int totalRecords = 0;

    var SeminarList = seminarRepository.GetSeminarDetail("CMP04").Seminar_Detail; //OrderBy(x => x.Seminar_Code).Skip(page * rows).Take(rows).ToList();

    var totalPages = (int)Math.Ceiling(totalRecords / (float)rows);
    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,

        rows = (
                   from s in SeminarList
                   select new
                   {
                       id = s.Seminar_Code + "__" + s.SpeakerID + "__" + s.TagID,
                       cell = new object[]
                                        {
                                            s.Seminar_Code,
                                            s.SpeakerID,
                                            s.TagID,
                                            s.DateAndTime
                                        }
                   }).ToArray()
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

public ActionResult ListingGridData(string sidx, string sord, int page, int rows)
{
    int currentPage = Convert.ToInt32(page) - 1;
    int totalRecords = 0;

    var SeminarList = seminarRepository.AllSeminarList().Seminar_Masters; //OrderBy(x => x.Seminar_Code).Skip(page * rows).Take(rows).ToList();

    var totalPages = (int)Math.Ceiling(totalRecords / (float)rows);
    var jsonData = new
    {
        total = totalPages,
        page,
        records = totalRecords,

        rows = (
                   from s in SeminarList
                   select new
                   {
                       id = s.Seminar_Code,
                       cell = new object[]
                                        {
                                            s.Seminar_Code,
                                            s.Title,
                                            s.Description,
                                            s.Room
                                        }
                   }).ToArray()
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}
}

更新了2

请让我更清楚地说明问题 我有两个表一次插入数据 1。SeminarMaster(Seminar_Code, Title, Description, Room)
[在Razor View] - 表格输入字段
2。SeminarDetail(Seminar_Code, SpeakerID, TagID, DateAndTime)
[在Razor View] - JQGrid行和列

这两个表是一对多的关系。

更新3
我真的很抱歉一次又一次地弄清楚我的问题 现在请再次阅读我的更新。我尽力让你清楚地理解我的问题。

Index.cshtml或首次显示表单,用户可以选择任何行并转到详细页面进行更新。但实际上,剃刀以下尚未完成。因为我需要在jqgrid的每一行添加Edit按钮。 该编辑按钮将通过解析所选代码重定向到我称为Detail.cshtml的另一个页面。我稍后会更新。这不是我的主要问题。

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Seminar List</h2>

<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
@Html.ActionLink("Click here", "Detail") to add new data.

<script type="text/javascript">
jQuery(document).ready(function () {        
    jQuery("#list").jqGrid({
        url: '/Seminar/ListingGridData/',
        editurl: "/Seminar/MyEdit/",
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Seminar_Code', 'Title', 'Description', 'Room'],
        colModel: [
                      { name: 'Seminar_Code', index: 'Seminar_Code', width: 40, align: 'left', editable: false },
                      { name: 'Title', index: 'Title', width: 40, align: 'left', editable: false },
                      { name: 'Description', index: 'Description', width: 40, align: 'left', editable: false },
                      { name: 'Room', index: 'Room', width: 40, align: 'left', editable: false }
                  ],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Title',
        sortorder: "asc",
        viewrecords: true,
        autowidth: true,
        autoheight: true,            
        caption: 'Seminar List - Grid'
    })
}); 
</script>

我们将不胜感激。

0 个答案:

没有答案