HTTP POST, GET in MVC? - asp.net-mvc-3

In MVC3, i created one register form. I created one model,,controller and view. This is my code:
[HttpGet]
public ActionResult Insert()
{
Models.Employees objEmpl = new Models.Employees();
return View(objEmpl);
}
[HttpPost]
[AcceptVerbs("Post")]
public ActionResult Insert(Models.Employees objs)
{
var v = new Models.test().InsertDl(objs);
return View();
}
The above is my controller
#model MvcVideo.Models.Employees
#{
ViewBag.Title = "Insert";
Layout = "~/Views/Shared/VideoLayout.cshtml";
}
<h2>Insert</h2>
#using(Html.BeginForm("Insert","Home",FormMethod.Post ))
{
<table>
<tr>
<td>
Employee Name
</td>
<td>
#Html.TextBox("Ename",#Model.Enames)
</td>
</tr>
<tr>
<td>
Department Id
</td>
<td>
#Html.TextBox("Departmentid",#Model.DepartId )
</td>
</tr>
<tr>
<td>
Email Id
</td>
<td>
#Html.TextBox("Emailid",#Model.EmailIds)
</td>
</tr>
<tr>
<td>
Address
</td>
<td>
#Html.TextBox("Address",#Model.Adress)
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;" >
<button title ="Ok" value="OK"></button>
</td>
</tr>
</table>
}
But the objs object at public actionresult Insert(models.Empoyees objs) action method parameter is showing null values. Imean Ename=Null, Department=0, Emailid=Null and Address=null

It isn't working because the names you've provided in your Html helpers don't match up with the property names on your model, so the default model binder can't resolve them when the values get posted back.
Using Html.TextBoxFor instead of Html.TextBox will provide you with strong typing against your model, and is the safer approach.

Replace this
#Html.TextBox("Ename",#Model.Enames)
With
#Html.TextBoxFor(model => model.Enames)
This will fix your issue.

Related

How To Send List Of Data From View To Controller With Ajax Form in Mvc4

How To Send List Of Data From View To Controller With Ajax Form In Mvc4 :
My Model Sample :
public class Food
{
public string name { get; set; }
public int quantity { get; set; }
}
public class CustomerModel {
public string CustomerName { get; set; }
public List<Food> ListFoods { get; set; }
}
[HttpPost]
public ActionResult AddCustomer(CustomerModel model)
{
//Add New Person
return Json(new { Result = "OK" });
}
My View :
#using (Ajax.BeginForm("AddCustomer", "Resturant", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = "Success"
}, new { #id = "frmaddCustomer" }))
{
<p>
<label>Customer Name </label>
</p>
<p>
#Html.CH_TextBox("CustomerName").size(TextBoxSize.Medium)
</p>
<p>
Food List
</p>
<table id="FoodList" class="static-table" data-bind='visible: gifts().length > 0'>
<thead>
<tr>
<td style="width: 250px;">Name</td>
<td>Quantity</td>
<td />
</tr>
</thead>
<tbody data-bind='foreach: gifts'>
<tr>
<td>
#Html.CH_TextBox("Food.name").size(TextBoxSize.Medium)
</td>
<td>
#Html.CH_TextBox("Food.quantity").size(TextBoxSize.Medium)
<td>
<a id="deleteRow">Delete</a>
</tr>
</tbody>
</table>
<button data-bind='click: addGift' type="submit">New Food</button>
<button data-bind='enable: gifts().length > 0' type="submit">Send Data</button>
}
I use the following method to post data to controller model :
<tr>
<td>
#Html.CH_TextBox("Food[0].name").size(TextBoxSize.Medium)
</td>
<td>
#Html.CH_TextBox("Food[0].quantity").size(TextBoxSize.Medium)
</td>
<td>
<a id="deleteRow">Delete</a>
</td>
</tr>
<tr>
<td>
#Html.CH_TextBox("Food[1].name").size(TextBoxSize.Medium)
</td>
<td>
#Html.CH_TextBox("Food[1].quantity").size(TextBoxSize.Medium)
</td>
<td>
<a id="deleteRow">Delete</a>
</td>
</tr>
<tr>
<td>
#Html.CH_TextBox("Food[2].name").size(TextBoxSize.Medium)
</td>
<td>
#Html.CH_TextBox("Food[2].quantity").size(TextBoxSize.Medium)
</td>
<td>
<a id="deleteRow">Delete</a>
</td>
</tr>
by using this method, my problem was solved.
Thanks .

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key "student". error

Iam using ASP.Net MVC3 Razor,
I have a controller where i binded the data
public Actionresult Index()
{
ViewBag.student= new SelectList(db.Collection, "ID", "Name");
ViewBag.student1= new SelectList(db.Collection, "ID", "Name");
return view();
}
This is my view
<table>
<tr>
<td>
#Html.DropDownList("student")
</td>
<td>
#Html.DropDownList("student1")
</td>
</tr>
</table>
What is the problem in my code, could any one help me ...
Thanx in advance..
public Actionresult Index()
{
ViewBag.studentList = new SelectList(db.Collection, "ID", "Name");
ViewBag.student1List = new SelectList(db.Collection, "ID", "Name");
return view();
}
<table>
<tr>
<td>
#Html.DropDownList("student", (SelectList)ViewBag.studentList)
</td>
<td>
#Html.DropDownList("student1", (SelectList)ViewBag.student1List)
</td>
</tr>
</table>

how to use webgrid and html helper method on the same view

In asp.net mvc3 project iam using ienumerable view to display collection of object in the webgrid. In the same view i am using html helper methods to create object. i got the error in the helper method "System.collection.generic.ienumerable<...> does not contain definition for <...>"
#model IEnumerable<TRADEBLOTTER_MVCPOC.Models.Trader />
#{
ViewBag.Title = "NewTrader";
}
#{
var grid = new WebGrid(source: Model,
defaultSort: "TradeID",
rowsPerPage: 5, fieldNamePrefix: "wg_",
canPage: true, canSort: true,
pageFieldName: "pg", sortFieldName: "srt" );
}
<h2>NewTrader</h2>
#using (Html.BeginForm(FormMethod.Post))
{
#Html.ValidationSummary()
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
#Html.LabelFor(t=>t.TraderName,"Trader Name")
</td>
<td>
#Html.TextBoxFor(t=>t.TraderName)
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Submit Trader" />
</td>
</tr>
</table>
}
I think typically you would use something like that in the model above your variable
[DisplayName("Trader Name")]
public string TraderName { get; set; }
and then your view
<h2>NewTrader</h2>
#using (Html.BeginForm(FormMethod.Post))
{
#Html.ValidationSummary()
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
#Html.LabelFor(t=>t.TraderName)
</td>
<td>
#Html.TextBoxFor(t=>t.TraderName)
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Submit Trader" />
</td>
</tr>
</table>
}
My suggestion: use a partial page for the grid (with the IEnumerable version of the model) and another partial page for the individual variables (with the single model).
If the grid is to display information only, and not to edit anything, then bind the grid to a method defined in the model - then there is no need for the view to have an IEnumberable model.

Paging in a scaffolding view MVC3

so using EF4 I create a scaffolding controller/view, so my question is how can I Add Paging to my view in easy/fast way??
the
controller generated
public ViewResult Index()
{
return View(db.Perifericos.ToList());
}
the
view: generated
#model IEnumerable<Paginacion.Models.Perifericos>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Nombre
</th>
<th>
Modelo
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Modelo)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.idPerifericos }) |
#Html.ActionLink("Delete", "Delete", new { id=item.idPerifericos })
</td>
</tr>
}
</table>
Maybe this can help you: http://www.asp.net/entity-framework/tutorials/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
You have to implement a paging in controller and in the view add code like the link that i have given to you.
I use this one http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/
It's support also ajax and areas
Easy to implement.
a complete msdn article with sample code
Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application

Post mvc3 table to controller action

I have a simple table
#using (Html.BeginForm("Save", "Subscription", FormMethod.Post))
{
<table>
<tr>
<th>
Name
</th>
<th>
Report
</th>
</tr>
#foreach (var item in Model.ReportSubscriptions)
{
<tr>
<td>
#Html.HiddenFor(item.Id)
#Html.TextBoxFor(item.Name)
</td>
<td>
#Html.TextBoxFor(item.Report)
</td>
</tr>
}
</table>
<button type="submit">Save</button>
}
The save action
public ViewResult Save(IEnumerable<ReportSubscription> list)
{
throw new NotImplementedException();
}
}
How can I get MVC to deserialize the post back to my model?
For collections you need to do a Editor template and use
#Html.EditorFor(m => m.Collection);

Resources