You need a form in your cshtml, and a parameter on your controller to receive the model. I usually use specially designed formmodels:
public class ProductFormModel
{
[ScaffoldColumn(false)]
public int ProductId { get; set; }
[Required, Display(Name = "Product")]
public string ProductName { get; set; }
}
Then the contoller will accept a ProductFormModel as a parameter:
[HttpPost]
public ActionResult Create(ProductFormModel model){
if(ModelState.IsValid){
//etc
}
}
The sample below is sending a model "PersonInfo" from Index.cshtml send to StudentController, received on HttpGet "create" send it to view create.cshtml.
This should work. Please let me know, if there are any issues.
Also, try using, the default Scaffolding feature which will create all the save,update and delete code automatically, where you can learn how these functions are populated.
Don't forget to "Mark As Answer" if a post helps you.
fredalsa
Member
6 Points
16 Posts
send a entity from cshtml to controller
May 23, 2012 08:18 PM|LINK
somebody help me sending a @model from cshtml to controller, to update that entity data.
vishwanath.s...
Member
172 Points
36 Posts
Re: send a entity from cshtml to controller
May 23, 2012 09:01 PM|LINK
You can do that by calling an action method on controller either by AJAX or form post, as simple as it is
http://www.asp.net/ajaxlibrary/jquery_posting_to.ashx
Or i did not understand your question, can you please elaborate?
Read more from my Blog
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: send a entity from cshtml to controller
May 23, 2012 09:01 PM|LINK
You need a form in your cshtml, and a parameter on your controller to receive the model. I usually use specially designed formmodels:
public class ProductFormModel { [ScaffoldColumn(false)] public int ProductId { get; set; } [Required, Display(Name = "Product")] public string ProductName { get; set; } }Then the contoller will accept a ProductFormModel as a parameter:
[HttpPost] public ActionResult Create(ProductFormModel model){ if(ModelState.IsValid){ //etc } }Web Pages CMS | My Site | Twitter
jsiahaan
Contributor
2550 Points
645 Posts
Re: send a entity from cshtml to controller
May 23, 2012 09:47 PM|LINK
Hi,
The sample below is sending a model "PersonInfo" from Index.cshtml send to StudentController, received on HttpGet "create" send it to view create.cshtml.
Here my Sender View send an object named "item":
@foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem => item.FullName)</td> <td>@Html.DisplayFor(modelItem => item.CategoryName)</td> <td>@Html.DisplayFor(modelItem => item.BirthDate)</td> <td>@Html.DisplayFor(modelItem => item.NationalIDNumber)</td> <td>@Html.DisplayFor(modelItem => item.AddressLine1)</td> <td>@Html.DisplayFor(modelItem => item.City)</td> <td style="padding-left: 15px"> @Html.ActionLink("Student", "Create", "Student", item, null) | @Html.ActionLink("Instructor", "Create", "Instructor", item, null) </td> </tr> }Here my controller receive the "item" named "PersonInfo personInfo", and send ViewData.Model and ViewBag.FullName:
// GET: /Student/Create public ActionResult Create(PersonInfo personInfo) { if (personInfo != null) { ViewBag.FullName = personInfo.FullName; CoursePerson student = new CoursePerson(); student.PersonID = personInfo.BusinessEntityID; student.EnrollmentDate = DateTime.Now; student.Discriminator = "Student"; ViewData.Model = student; } return View(); }Here my receiver view as "model" from "ViewData.Model" and @ViewBag.FullName
@model CourseCenter.Models.CoursePerson @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Student</legend> @Html.HiddenFor(model => model.PersonID) @Html.HiddenFor(model => model.Discriminator) <div class="editor-label"> @Html.LabelFor(model => model.PersonInfo.FullName) @ViewBag.FullName </div> <div class="editor-label"> @Html.LabelFor(model => model.EnrollmentDate) </div> <div class="editor-field"> @Html.EditorFor(model => model.EnrollmentDate) @Html.ValidationMessageFor(model => model.EnrollmentDate) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>Hope this can help.
Indonesian Humanitarian Foundation
fredalsa
Member
6 Points
16 Posts
Re: send a entity from cshtml to controller
May 24, 2012 12:09 AM|LINK
Thanks a lot Siahaan.
My problem is when I click the submit button. How I can send the all field that I have in the cshtml.
fredalsa
Member
6 Points
16 Posts
Re: send a entity from cshtml to controller
May 24, 2012 12:34 AM|LINK
Here is an example:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Editar Companias</legend>
<table align="center" style="background-color:White">
<tr>
<td>
@Html.LabelFor(model => model.Codigo)
</td>
<td>
@Html.TextBoxFor(model => model.Codigo)
</td>
</tr>
<tr>
<td style="width:auto">
@Html.LabelFor(model => model.Descripcion)
</td>
<td style="left: 5px">
@Html.TextBoxFor(model => model.Descripcion, new {@class="myClass", size=50 })
@Html.ValidationMessageFor(model => model.Descripcion)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Direccion_1)
</td>
<td>
@Html.TextBoxFor(model => model.Direccion_1, new {@class="myClass", size=50 })
@Html.ValidationMessageFor(model => model.Direccion_1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Direccion_2)
</td>
<td>
@Html.TextBoxFor(model => model.Direccion_2, new {@class="myClass", size=50 })
@Html.ValidationMessageFor(model => model.Direccion_2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Direccion_3)
</td>
<td>
@Html.TextBoxFor(model => model.Direccion_3, new {@class="myClass", size=50 })
@Html.ValidationMessageFor(model => model.Direccion_3)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.RNC)
</td>
<td>
@Html.TextBoxFor(model => model.RNC, new { @class = "myClass", size = 15 })
@Html.ValidationMessageFor(model => model.RNC)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Telefono_1)
</td>
<td>
@Html.TextBoxFor(model => model.Telefono_1, new {@class="myClass", size = 15 })
@Html.ValidationMessageFor(model => model.Telefono_1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Telefono_2)
</td>
<td>
@Html.TextBoxFor(model => model.Telefono_2, new { @class = "myClass", size = 15 })
@Html.ValidationMessageFor(model => model.Telefono_2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Fax_1)
</td>
<td>
@Html.TextBoxFor(model => model.Fax_1, new {@class="myClass", size=15 })
@Html.ValidationMessageFor(model => model.Fax_1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Fax_2)
</td>
<td>
@Html.TextBoxFor(model => model.Fax_2, new {@class="myClass", size=15 })
@Html.ValidationMessageFor(model => model.Fax_2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Presidente)
</td>
<td>
@Html.TextBoxFor(model => model.Presidente, new {@class="myClass", size=50 })
@Html.ValidationMessageFor(model => model.Presidente)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Email)
</td>
<td>
@Html.TextBoxFor(model => model.Email, new {@class="myClass", size=100 })
@Html.ValidationMessageFor(model => model.Email)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Status)
</td>
<td>
@Html.TextBoxFor(model => model.Status, new {@class="myClass", size=1 })
@Html.ValidationMessageFor(model => model.Status)
</td>
</tr>
<tr>
<td class="actionButtons">
<p>
<input type="submit" value="Actualizar"/>
</p>
</td>
<td class="actionButtons">
@Html.ActionLink("Regresar a Lista", "Index")
</td>
</tr>
</table>
</fieldset>
}
I don't know how can I send those fields to the controller to call another class that update the data.
I have:
[HTTPPOST}
public ActionResult Editar()
{
but here I don't know how I can referencing the model that I have in the cshtml
}
CPrakash82
All-Star
18720 Points
2899 Posts
Re: send a entity from cshtml to controller
May 24, 2012 01:09 AM|LINK
You Html.BeginForm() should include the controller and action name which will get invoked after form post, something like this.
@Html.BeginForm("Action", "Controller", FormMethod.Post)
and you action method should have the model type as an argument
public ActionResult Editar([ModelType] model)
{
}
Thanks
abdu292
Participant
1555 Points
372 Posts
Re: send a entity from cshtml to controller
May 24, 2012 01:25 AM|LINK
public ActionResult(Editar editar) { if(ModelState.IsValid) { db.Editars.Add(editar); db.SaveChanges(); return RedirectToAction("Index"); } }This should work. Please let me know, if there are any issues.
Also, try using, the default Scaffolding feature which will create all the save,update and delete code automatically, where you can learn how these functions are populated.
With best regards,
fredalsa
Member
6 Points
16 Posts
Re: send a entity from cshtml to controller
May 24, 2012 02:24 AM|LINK
Thanks everybody to help me on this.
CPrakash82, your example works fine on my code.
Appologize because I'm working on a DB2 database and I can't take advantage from LINQ and SQL server entity framework.
Again, thanks a lot, all of you.
CPrakash82
All-Star
18720 Points
2899 Posts
Re: send a entity from cshtml to controller
May 24, 2012 02:38 AM|LINK
Glad, that it worked for you.
Thanks,
Please mark the replies as answers if they help.