Günaydın,Simple ASP.NET MVC 2 Soru - Edit Iskele Kullanımı
Düzenleme iskelesini kullanıyorum. İşte iki kod bittir: Kontrolör Kodu:
public ActionResult Edit(int id)
{
var viewModel = new ListingManagerViewModel
{
Listing = AfvClassifiedsDB.Listings.Single(l => l.ListingID == id),
Categories = AfvClassifiedsDB.Categories.ToList(),
};
return View(viewModel);
}
//
// POST: /ListingManager/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var listing = AfvClassifiedsDB.Listings.Single(l => l.ListingID == id);
try
{
// Save the changes to Listing.
UpdateModel(listing, "Listings");
AfvClassifiedsDB.SaveChanges();
return RedirectToAction("Index");
}
catch
{
// An error has occured so redisplay the form instead.
var viewModel = new ListingManagerViewModel
{
Listing = listing,
Categories = AfvClassifiedsDB.Categories.ToList(),
};
return View(viewModel);
}
}
Görünüm Kodu:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<AfvClassifieds.ViewModels.ListingManagerViewModel>" %>
<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>
<%: Html.EditorFor(model => Model.Listing, new { Categories = Model.Categories })%>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
Bu iş gibi görünüyor, ancak form gönderdiğinde, değerler değişmez. Bu, MVC Müzik Mağazası kullanılarak bir rehber olarak oluşturulmuştur. Hata yok, ancak oluşturduğum form değerleri gönderilmiyor.
teşekkürler cevap için, ben bu yazının kapalı bu çözümü esas aldık. Geriye baktım ve spot görünüyor, UpdateModel() Metodu MVC içine inşa edildiğini düşündüm? – JHarley1
ViewModel kullanıyor musunuz? bu bağlantıyı kontrol edin http://www.joe-stevens.com/2010/02/17/asp-net-mvc-using-controller-updatemodel-when-using-a-viewmodel/ – Arief