I am trying to call Action method "Createdirect" on form submission from Index.cshtml view.
I want to list and create in the same view. Code works for list..it displays data, but when trying to create, it does not pass form data to action method.. It passes null values as shown in screenshot attached..
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; }
}
}