ASP.Net MVC在同一页面上使用相同模型的多个表单

时间:2016-04-27 20:24:13

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

非常新的ASP.Net,从PHP迁移。所以我很确定我已经打破了本书中的所有规则,但无论如何,这是我想要完成的。

我有一个页面,允许我们的停车场(停车场)操作员在任何给定时间更新停车场的车辆数量。

我想要实现的是有一个页面列出了停车场及其各自可用的停车位和使用过的停车位。我正在使用jQuery来制作它,以便当例如更新自由空间文本框时,使用的空间相应地调整自身。例如,在一个有100个停车位的停车场,当有人在自由空间框中输入10个时,使用过的空间框会自动更新为90.

表单示例:

form format

所以我不得不做一些巫术(我怀疑是错误的)来完成这项工作(即为每个文本字段添加动态ID,以便我知道在onblur事件期间要更新哪些文本框。

无论如何这一切都很好但是我的验证不起作用。我怀疑这是由于我动态生成表格。

这是我的观看代码:

  model IEnumerable<WebApplication1.Models.ParkingCount>

@{
    ViewBag.Title = "Parking Lot Administration";
}

<h2>Parking Lots</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.parkingLotName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.parkingSpaces)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.freeSpaces)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.usedSpaces)
        </th>
        <th></th>
    </tr>
@{int i = 0;}
@foreach (var item in Model)
{
    using (Html.BeginForm("UpdateParkingLot", "ParkingCount", FormMethod.Post, new { @Id = "Form" + i}))

    {
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <tr>
        <td>
            @Html.HiddenFor(modelItem => item.ID)
            @Html.DisplayFor(modelItem => item.parkingLotName)

        </td>
        <td>          
            @Html.TextBoxFor(modelItem => item.parkingSpaces, new { id = "parkingSpaces" + i })
            @Html.ValidationMessageFor(modelItem => item.parkingSpaces, "", new { @class = "text-danger" })
        </td>
        <td>
            @Html.TextBoxFor(modelItem => item.freeSpaces, new { id = "freeSpaces" + i })
            @Html.ValidationMessageFor(modelItem => item.freeSpaces, "", new { @class = "text-danger" })
        </td>
        <td>
            @Html.TextBoxFor(modelItem => item.usedSpaces, new { id = "usedSpaces" + i })
            @Html.ValidationMessageFor(modelItem => item.usedSpaces, "", new { @class = "text-danger" })
        </td>
        <td>
            <button type="submit">Update</button>
        </td>
    </tr>

    }
    i++;


}

</table>

@for (int j = 0; j <= i; j++)
{


<script>

    $("#parkingSpaces0").blur(function () {


        var i = @i;

        var totalSpacesValue = $(this).val();
        var usedSpacesValue = $("#usedSpaces0").val();


        $("#freeSpaces0").val(totalSpacesValue - usedSpacesValue);

    }
    )

    $("#freeSpaces0").blur(function () {

    var totalSpacesValue = $("#parkingSpaces0").val();
    var freeSpacesValue = $("#freeSpaces0").val();

        $("#usedSpaces0").val(totalSpacesValue - freeSpacesValue);


})

    $("#usedSpaces0").blur(function () {

    var totalSpacesValue = $("#parkingSpaces0").val();
    var usedSpacesValue = $("#usedSpaces0").val();

        $("#freeSpaces0").val(totalSpacesValue - usedSpacesValue);


    })

</script>
}

这是我的型号代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace WebApplication1.Models
{
    public class ParkingCount { 

        public int ID { get; set; }

        [Display(Name="Parking Lot Name")]
        public string parkingLotName { get; set; }

        [Required]
        [Display(Name ="Total Parking Spaces")]
        public int parkingSpaces { get; set; }

        [Required]
        [Display(Name ="Free Parking Spaces")]
        public int freeSpaces { get; set; }

        [Required]
        [Display(Name = "Used Parking Spaces")]
        public int usedSpaces { get; set; }

    }
}

最后我的(不完整的)控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CsvHelper;
using System.IO;


namespace WebApplication1.Controllers
{

    public class ParkingCountController : Controller
    {
        // GET: ParkingCount
        public ActionResult Index()
        {

            //create an instance of our service class
            WebApplication1.ServiceClasses.GetParkadeDetails myCounts = new WebApplication1.ServiceClasses.GetParkadeDetails();

            //create list objec to store parkade objects
            List<WebApplication1.Models.ParkingCount> myList = new List<WebApplication1.Models.ParkingCount>();

            //read in the CSV files of parking lots (c:\parkingLots.csv)
            StreamReader sr = new StreamReader("c:\\parkingLots.csv");
            CsvReader csvRead = new CsvReader(sr);
            IEnumerable<WebApplication1.ServiceClasses.ParkingLotRecord> lines = csvRead.GetRecords<WebApplication1.ServiceClasses.ParkingLotRecord>();

            //add the parking lots to our list
            foreach (var line in lines)

                myList.Add(new Models.ParkingCount
                {
                    ID = line.parkingLotID,
                    parkingLotName = line.parkingLotName,
                    freeSpaces = myCounts.getFreeStalls(line.parkingLotID),
                    parkingSpaces = myCounts.getStallCount(line.parkingLotID),
                    usedSpaces = myCounts.getUsedStalls(line.parkingLotID)

                });

            return View(myList);
        }

        public ActionResult UpdateParkingLot(Models.ParkingCount item)
        {

            try
            {
                //Update the parking counters here
                int parkingLotID = item.ID;
                int freeSpaces = item.freeSpaces;

                System.Diagnostics.Debug.WriteLine(freeSpaces + " free spaces at Parking Lot " + parkingLotID);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }


        }


    }
}

知道如何让我的验证工作吗?

感谢您提前提供任何帮助。

约翰。

0 个答案:

没有答案