ASP.NET MVC - 使用复选框更新多对多关系

时间:2012-12-13 11:26:21

标签: asp.net-mvc razor

我在一个MVC项目中,目前仍处于多对多映射状态。

我有很多关系。它模拟了大学中的学生课程选择。学生有科目,在给定的学期,有完整的科目列表。

需要:制作一个学生可以访问该页面的表格,并通过使用复选框选择一些科目。

我创建了一个View Modle(至少这就是我所说的)。它是将在“查看”视图中使用的模型。

Edit.cshtml

//in the Edit View
@model StudentViewModel

//Some more code
@Html.EditorFor(model => model.Std.Name)
//etc..

学生视图模型

class StudentViewModel
{
public Student Std { get; set; }
public List<SubjectsSelection> Subs{ get; set; }
}

class SubjectSelection
{
public string Name { get; set; }
public int ID { get; set; }
public bool Selected { get; set; }
}

我想要的是为每个主题添加一组复选框,以便检查Selected = true

我打算手动通过for循环创建一个复选框列表。

我是否可以使用类似@Html.EditorFor(model => model.Subs)的内容来执行此操作,以便在提交表单时我可以使用这样的控制器

[HttpPost]
public ActionResult Edit(Machine machine, SubjectSelection[] subs)
{
//Some code here
}

在那之后,我将能够进行必要的连接(至少希望如此)并保存到数据库中。

1 个答案:

答案 0 :(得分:0)

示例显示如何将数据发布到控制器方法

剃刀代码:

<div id="form">
        <table width="600">
            <tr>
                <td>Select Date:</td>
                <td>
                    <input class="txtDate" type="date" size="20"></td>
            </tr>
            <tr>
                <td>Select Expense Type:</td>
                <td>
                    <select class="ExpenseType">
                        <optgroup label="Room">
                            <option>Room Fare</option>
                        </optgroup>

                        <optgroup label="Mess">
                            <option>Monthly Mess</option>
                        </optgroup>

                        <optgroup label="Others">
                            <option>Bus Fare</option>
                            <option>Tapari</option>
                            <option>Mobile Recharge</option>
                            <option>Auto</option>
                        </optgroup>
                    </select></td>
            </tr>
            <tr>
                <td>Enter Cost:</td>
                <td>
                    <input  class="cost" type="text" size="45" /></td>
            </tr>
            <tr>
                <td>Extra Details:</td>
                <td>
                    <input class="extra" type="text" size="45" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <button href="javascript:void(0);" onClick="saveExpense();" >Submit</button></td>
            </tr>
        </table>
    </div>

<script>
function saveExpense()
    {
        var expenseobject = {          //MyModel Expense has these fields
            date:$('.txtDate').val() ,
            type:$('.ExpenseType').val() ,
            cost: $('.cost').val(),
            extra:$('.extra').val()

        };

        $.ajax({
            url: './saveexpense',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ obj: expenseobject }),
            success: function (result) {
                handleData(result);

            }
        });

    }
</script>

<强>控制器:

 public ActionResult SaveExpense(Expense obj)
        {
            obj.ExpenseId = Guid.NewGuid();
            if (ModelState.IsValid)
            {
                context.expenses.Add(obj);
                context.SaveChanges();
                int total=context.expenses.Sum(x => x.cost);
                return Json(new {spent=total,status="Saved" });

            }
            else
                return Json(new { status="Error"});   
        }

您可以执行以下操作,而不是制作Model的对象:

您可以使用jquery直接serialize()表单并将数据发布到控制器......然后就完成了