MVC sayfam hem Ekle hem de Güncelle için kullanılır. ,MVC Nasıl kullanılır? Görünüm, verileri kaydetmek için gerekenden fazla olduğunda Model modelini görüntüle
"No parameterless constructor defined for this object."
MissingMethodException: No parameterless constructor defined for this object.]
Bu paramaterless yapıcısı vuracak şu anda dışarı yorum: Şimdiye kadar veri göstermeye ancak Gönder düğmesine tıklandığında, bu hatalar için çalışıyor. Viwemodel üzerinde yalnızca görüntüleme ve ekleme/güncelleme için gerekli olmayan 50 ek alan vardır. Formda, görünüm modelinden daha az form alanı vardır.
Bu durumu ele alan bir çalışma kodu örneği var mı? Ben çalışabilir burada kabul cevabı gördük, ama bunu yapmak yeniyim beri tam bir çalışma örneği gerekir:
ASP.NET MVC - Proper usage of View Model and Command pattern
<form action="/incident/SaveIncident" method="post">
<div>
<input class="btn btn-primary" type="submit" value="SubmitSave" />
<br />
<select id="drpSite">
<option value="0">Pick a site...</option>
@foreach (var site in Model.Sites)
{
<option value="@site.SiteId">
@site.SiteName
</option>
}
</select>
<br />
upsert:@Html.TextBoxFor(model => model.objIncidentUpsert.UpsertBemsId, new { @class = "form-control" })
<br />
viewBOIncDesc1: @Html.TextBoxFor(model => Model.objIncident.IncidentModel.IncidentDescription1, new { @class = "form-control" }))
</div>
</form>
IncidentViewModel.cs
public class IncidentViewModel
{
public int IncidentId { get; set; }
public IncidentModelBO objIncident { get; set; }
public IncidentModelUpsert objIncidentUpsert { get; set; }
public IncidentViewModel(int incidentId)
{
if (incidentId == 0)
{
objIncident = new IncidentModelBO();
}
else
{
IncidentId = incidentId;
objIncident = IncidentModelBO.Get(incidentId);
}
}
public IEnumerable<SiteModel> Sites {get; set;}
}
IncidentController.cs:
//[HttpPost]
//[ActionName("SaveIncident")]
//public ActionResult SaveIncident()
//{
// return new HttpStatusCodeResult(400, "Problem with inputxxx ");
//}
[HttpPost]
[ActionName("SaveIncident")]
public ActionResult SaveIncident(IncidentViewModel objIncidentBO)
{
string loggedinbemsid = System.Web.HttpContext.Current.Session[Utility.SessionKeyIndex.BEMSID.ToString()].ToString();
try
{
objIncidentBO.objIncident.Create(loggedinbemsid, null);
IncidentViewModel vwIncidentData = new IncidentViewModel(objIncidentBO.objIncident.IncidentModel.IncidentId);
return View("index", vwIncidentData);
}
catch (Exception e)
{
return new HttpStatusCodeResult(400, "Problem with input: " + e.Message);
}
, çünkü defaultModelBinder 'Eğer sahip değilseniz istisna atar' Activator.CreateInstance() 'kullanarak modeli (ler) başlatır. Fakat modelinizin ismine rağmen, MVC bağlamında bir görünüm modeli değildir. Bir görünüm modeli veri modelleri içermemeli ve hiçbir zaman veritabanı erişim kodu içermemelidir. [MVC'de ViewModel nedir?] (Http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –
neleri okuduğumu önerdiğinizde, neyi kaçırdığımı açıkladığınız için çok teşekkür ederim. Öğrenecek çok şeyim var. – Remy