ASP.Net MVC Easy Data Editing from Default Template

when you create a new view and select an edit template, the template will create a text box for the primary key, which is not editable.

   <%=Html.TextBox("CompanyID", Model.CompanyID)%>

      

Thus, removing the control from view will cause a problem: the collection that is the post controller has CompanyID = 0, so no editing will be done. But if I return this line of code for viewing, then the PropertyID in the published collection has the correct value. I really love this tutorial: http://www.asp.net/learn/mvc/tutorial-21-vb.aspx , and there (the gray box above "Listing 6 - Controllers \ HomeController.vb (Edit methods)"), it says you can remove the control but its not working .. any tips?

Update Ok, for a more detailed explanation, here comes 2 problems:
problem 1 in this code:

<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal movieToEdit As Movie) As ActionResult

      

if you remove the MovieID textbox from the viewer that the movie in the movietoedit collection is always zero, so the tutortial from the asp.net site won't work! if i want to remove the movieID from the view i have to pass the ID to my controller like this:

 <AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal ID as integer,ByVal movieToEdit As Movie) As ActionResult

      

now i can query the model using this id. This is not a problem, but I need to figure out several times as I did step by step from the site!

Issue 2 So how do you make an edit without using the movieID, even in a hidden textarea?
I am using this as my controller: (using the Enity framework)

  <AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal id As Integer, ByVal collection As Company) As ActionResult
    If Not ModelState.IsValid Then
        Return View()
    End If

    Try
        Dim c = _db.CompanySet.FirstOrDefault(Function(m) m.CompanyID = id)

        If c Is Nothing Then
            Return RedirectToAction("index")
        End If



        _db.ApplyPropertyChanges(c.EntityKey.EntitySetName, collection)
        _db.SaveChanges()

        Return RedirectToAction("Index")
    Catch ex As Exception
        Throw ex
    End Try
End Function

      

And if I remove the CompanyID from my view (textbox or hidden field) it gives me this error from c.EntityKey.EntitySetName:
ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type "Company. Company".
and if i add something like

  <%= Html.TextBox("CompanyName", Model.CompanyName) %>

      

it will work fine .. i'm confused!

note: I have checked the code more than 10 times and I am wondering if there is something I am doing wrong or the lesson is wrong.

0


source to share


3 answers


The tutorial is right, you shouldn't leave a textbox with an ID, I think they assume the ID is part of your route like / movies / edit / 1. If it isn't, you can add it to the form action using the routes collection or add a hidden field to the form.

Adding an id to the route of the edit action:

    <% using (Html.BeginForm("Edit", "Controller", 
              new {id = Model.Id}, FormMethod.Post))
   { %>

      



Or a hidden field:

 <%=Html.Hidden("CompanyID", Model.CompanyID)%>

      

+2


source


If you are passing in an ID and updating specific fields via UpdateModel, it should work. You should better place your Edit method so we can see what's going on. The following example can illustrate the process I was trying to explain.



[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues) {

    Page page = repo.GetPage(id); // returns page with specific id
    UpdateModel(page); // updates page with new form values
    repo.Save(); // saving all the changes
    return RedirectToAction("Details", new { id = page.id });

}

      

0


source


Try this way:

<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(<Bind(Exclude:="Id")> ByVal movieToEdit As Movie) As ActionResult

      

For more information on ASP.Net MVC see http://asp.net/learn/mvc

0


source







All Articles