When I Click the Save Button on the Edit.aspx it call the Edit() but the Model on the page isn’t pass to the parameter, it just null.
When I try to save the change, the Products model won’t pass to the edit(). I using a Strongly-Typed View How do I get it to pass the Type to the Edit(Products) method?
How's is that going to solve the problem? My issue is when I Click save button the data on in view won't save ,because when the Edit() method is call the model is not pass to the Edit() methods. It just null. I'm assuming that mvc does this for you automatically.
For some reason mine won't work
My issue is when I Click save button the data on in view won't save ,because when the Edit() method is call the model is not pass to the Edit() methods. It just null.
From your code, it can be just only AFTER you submit , make an error in the Edit action and return View WITHOUT a model.
duce1nik
Member
12 Points
11 Posts
Save change when click Save button on edit.aspx
Dec 05, 2012 09:26 PM|LINK
When I Click the Save Button on the Edit.aspx it call the Edit() but the Model on the page isn’t pass to the parameter, it just null.
When I try to save the change, the Products model won’t pass to the edit(). I using a Strongly-Typed View How do I get it to pass the Type to the Edit(Products) method?
Here the code I have.
public ActionResult Edit(int id)
{
return View(GetProductID(id));
}
//
// POST: /Products/Edit/5
[HttpPost]
public ActionResult Edit(Products id)
{
try
{
// TODO: Add update logic here
MyProductsEntity be = new MyProductsEntity();
Products origProduct = GetProductID(id.ProductID);
be.Products.Attach(id);
be.ApplyOriginalValues("Products", origProduct);
be.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
Here the View.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProductsMVC.Models.Products>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.ProductID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ProductID) %>
<%: Html.ValidationMessageFor(model => model.ProductID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Save change when click Save button on edit.aspx
Dec 06, 2012 01:50 AM|LINK
please modify
catch(Exception ex)
{
ModelState.AddMOdelError("",ex.Message)
return View(id);
}
duce1nik
Member
12 Points
11 Posts
Re: Save change when click Save button on edit.aspx
Dec 06, 2012 09:23 AM|LINK
How's is that going to solve the problem? My issue is when I Click save button the data on in view won't save ,because when the Edit() method is call the model is not pass to the Edit() methods. It just null. I'm assuming that mvc does this for you automatically. For some reason mine won't work
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Save change when click Save button on edit.aspx
Dec 06, 2012 02:43 PM|LINK
From your code, it can be just only AFTER you submit , make an error in the Edit action and return View WITHOUT a model.
Please try my code. It won't hurt - au contraire
duce1nik
Member
12 Points
11 Posts
Re: Save change when click Save button on edit.aspx
Dec 06, 2012 06:16 PM|LINK
When I try the code I it return a edit.aspx with empty fields and the following code "Object reference not set to an instance of an object."
When I try to Change the Post Edit(int id) I get the ID value. If I try Edit(Products id) it null the object is not pass.
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Save change when click Save button on edit.aspx
Dec 07, 2012 03:24 AM|LINK
Did you try with GET or with POST?
duce1nik
Member
12 Points
11 Posts
Re: Save change when click Save button on edit.aspx
Dec 07, 2012 04:09 AM|LINK
Post
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Save change when click Save button on edit.aspx
Dec 07, 2012 06:11 AM|LINK
and how do you know it's null? Did you set a breakpoint?
duce1nik
Member
12 Points
11 Posts
Re: Save change when click Save button on edit.aspx
Dec 07, 2012 11:41 AM|LINK
ignatandrei
All-Star
135073 Points
21662 Posts
Moderator
MVP
Re: Save change when click Save button on edit.aspx
Dec 07, 2012 12:37 PM|LINK
could you post somewhere the project?( without database - load data from memory)