表单没有将值传递给action方法

时间:2018-02-20 10:17:45

标签: c# asp.net-mvc

我试图在Index.cshtml视图的表单提交上调用Action方法“Createdirect”。

我想在同一视图中列出并创建。代码适用于list ..it显示数据,但在尝试创建时,它不会将表单数据传递给action方法..它传递空值,如附带的屏幕截图所示。

Index.cshtml

    @using CRUD_Entity_DataFirst.Models
@model Tuple<Customer_MVC,IEnumerable<Customer_MVC>>
@{
    ViewBag.Title = "Index";
}

<h4>Customers</h4>

<link href="@Url.Content("~/Content/table.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/select.js")"></script>
<script src="@Url.Content("~/Scripts/table.js")"></script>

@Html.DropDownList("searchby", new SelectList(Enum.GetValues(typeof(search))), "- - Search By - -")
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search ..">
<label id="bdy" style="color:red"></label>

<table class="table" id="myTable">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Item1.First_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.Last_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.Mobile)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.Address_Temp)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.Address_Perm)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.State)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.Zipcode)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model.Item2)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.First_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Last_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mobile)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address_Temp)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address_Perm)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.State)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Zipcode)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("View", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { onclick = "return confirm('Are you sure wants to delete?');" })
            </td>
        </tr>
    }
</table>

@using (Html.BeginForm("Createdirect","Customer",FormMethod.Post))
{
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.First_Name, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.First_Name, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.Last_Name, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.Last_Name, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.Email, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.Email, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.Mobile, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.Mobile, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.Address_Temp, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.Address_Temp, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.Address_Perm, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.Address_Perm, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.DropDownListFor(model => model.Item1.State, new SelectList(Enum.GetValues(typeof(States))), "State", new { @class = "form-control", @style = "width:80px" })
                    @Html.ValidationMessageFor(model => model.Item1.State, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.DropDownListFor(model => model.Item1.City, new SelectList(Enum.GetValues(typeof(Cities))), "City", new { @class = "form-control", @style = "width:80px" })
                    @Html.ValidationMessageFor(model => model.Item1.City, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.EditorFor(model => model.Item1.Zipcode, new { htmlAttributes = new { @class = "form-control", @style = "width:80px" } })
                    @Html.ValidationMessageFor(model => model.Item1.Zipcode, "", new { @class = "text-danger" })
                 </div>

                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>

}


<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

CustomerController.cs

// GET: Customer
        public ActionResult Index()
        {
            if (Session["User"] == null)
            {
                return RedirectToAction("Login");
            }
           return View(Tuple.Create<Customer_MVC,IEnumerable<Customer_MVC>>(new Customer_MVC(),vd.Customer_MVC.ToList()));

        }

//create:post
        [HttpPost]
        public ActionResult Createdirect(Customer_MVC custcreate)
        {
            if (ModelState.IsValid)
            {
                vd.Customer_MVC.Add(custcreate);
                vd.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(custcreate);
        }

Customer_MVC.cs

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CRUD_Entity_DataFirst.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    [MetadataType(typeof(CustomersValid))]
    public partial class Customer_MVC
    {
        public int Id { get; set; }
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
        public string Address_Temp { get; set; }
        public string Address_Perm { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public Nullable<int> Zipcode { get; set; }
    }
}

here it showing null values

1 个答案:

答案 0 :(得分:0)

使用foreach循环时,模型绑定不起作用,因为html控件没有使用有意义的ID进行渲染。

将循环更改为for循环:

@for (int i = 0; i < Model.Item2.Length; i++)
{
    <tr>
        <td>
            @Html.DisplayFor(model => model.Item2[i].First_Name)
        </td>
        <td>
...

您可能还有一个问题(但首先尝试),因为IEnumerable不是绑定点的实例。如果是这种情况,那么唯一的解决方案是将基于元组的模型更改为类实现:

public class MyModel
{
    public Customer_MVC Item1 { get; set; }
    public IEnumerable<Customer_MVC> Item2 { get; set; } = new List<Customer_MVC>();
}