Both Create and List in the Same View MVC4 With Database Support RSS

Models

namespace MvcApplication4.Models

{
[Table("Cust_det")]

public
class CustomerModel
{
[Required]

public int Id { get; set; }

[Required]
public
string FirstName { get; set; }
[Required]

public
string LastName { get; set; }
}

}

namespace
MvcApplication4.Models
{

public
class CustomerDetailModel
{

public
CustomerModel Customer { get; set; }
public
IEnumerable<CustomerModel> Customers { get; set; }
}

}

namespace
MvcApplication4.Models
{

public
class CustomerContext : DbContext
{

public
DbSet<CustomerModel> Cust { set; get; }
}

}

Controller 

namespace MvcApplication4.Controllers

{
public
class CustomerController : Controller
{

//

// GET: /Customer/

public ActionResult Index()

{

CustomerContext db = new CustomerContext();

var model = new CustomerDetailModel();
model.Customers = (List<CustomerModel>)db.Cust.ToList();

return
View(model);
}

[HttpPost]

public ActionResult Create(CustomerDetailModel model)
{

CustomerContext db = new CustomerContext();

if
(model.Customer != null)
{

var
customer = new CustomerModel()
{

Id = model.Customer.Id,

FirstName = model.Customer.FirstName,

LastName = model.Customer.LastName

};

string connectionString = ConfigurationManager.ConnectionStrings["CustomerContext"].ConnectionString;

using (SqlConnection con = new SqlConnection(connectionString))
{

SqlCommand cmd = new SqlCommand("spAddCustomer", con);

cmd.CommandType =System.Data.CommandType.StoredProcedure;
con.Open();
SqlParameter paramEID = new SqlParameter();

paramEID.ParameterName = "@Id";

paramEID.Value = customer.Id ;

cmd.Parameters.Add(paramEID);

SqlParameter parammname = new SqlParameter();

parammname.ParameterName = "@Fname";

parammname.Value = customer.FirstName ;
cmd.Parameters.Add(parammname);
SqlParameter paramName = new SqlParameter();

paramName.ParameterName = "@Lname";

paramName.Value = customer.LastName;

cmd.Parameters.Add(paramName);

cmd.ExecuteNonQuery();

}

var
customerList = new List<CustomerModel>();
customerList = (List<CustomerModel>)db.Cust.ToList();

customerList.Add(customer);

}

return
RedirectToAction("Index");
}

}

}

//
 

View

@model MvcApplication4.Models.CustomerDetailModel

@{
ViewBag.Title = "Index";

}

@{ using (@Html.BeginForm("Create", "Customer", FormMethod.Post))

{

<h2>

Manage Customers</h2>

<div id="divCustomerCreate">

<h3>

Create Customer</h3>

<label>

ID :</label>

@Html.TextBoxFor(m => m.Customer.Id)

<br />

<label>

First Name:</label>

@Html.TextBoxFor(m => m.Customer.FirstName)

<br />

<label>

Last Name:</label>

@Html.TextBoxFor(m => m.Customer.LastName)

<br />

<input type="submit" value="Create" />

</div>

<div id="divCustomerList">

<h3>

Customers List</h3>

<ul>

@if (Model.Customers != null && Model.Customers.Any())

{

foreach
(var customer in Model.Customers)
{

<li>

@customer.Id, @customer.LastName, @customer.FirstName

</li>

}

}

</ul>

</div>

}

}
Ebook Download
View all
Learn
View all