TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Praveen Kumar
NA
235
22.2k
When i am trying to change some values update not happening
Apr 11 2020 2:28 AM
Edit.cshtml
@model Redi2.RMIM.Web.Areas.AccountSetup.Models.ContactDetailViewModel
@{
ViewBag.Title = "Edit Contact";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Edit", "Contact", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<div class="row">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group col-md-6">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
</div>
</div>
@for (var i = 0; i < Model.Attributes.Count; i++)
{
<div class="form-group col-md-6">
@Html.HiddenFor(model => model.Attributes[i].AddressValueName)
@Html.Label(@Html.DisplayFor(model => model.Attributes[i].AddressValueName).ToString(), htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Attributes[i].AddressValue, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Attributes[i].AddressValue, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Attributes[i].AddressElement)
</div>
</div>
}
</div>
<div class="col-md-offset-2">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
@Scripts.Render("~/bundles/jqueryval")
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
------------------------------------------------------------------------------------------------------------------------------------
Controller
[HttpGet]
public ActionResult Edit(int? id)
{
if(id==null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())
{
var model = new ContactDetailViewModel();
model = _ContactRepo.GetContact(id.Value);
return PartialView(model);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ContactDetailViewModel model)
{
try
{
using (var dbContextScope = _dbContextScopeFactory.Create())
{
Contact contact = model;
User user = _Login.GetCurrentUser();
_ContactRepo.UpdateContact(contact, user);
dbContextScope.SaveChanges();
}
}
catch (RMException rme)
{
rme.Log();
ModelState.AddModelError("", "Saving contact failed. Please check your values and try again.");
return PartialView(model);
}
catch (Exception)
{
ModelState.AddModelError("", "Saving contact failed for an unknown reason. Please check your values and try again.");
return PartialView(model);
}
return RedirectToAction("Index", "Contact");
}
Model Codes
public Contact UpdateContact(Contact contact, User user)
{
var dbContact = DbContext.Contacts.Find(contact.Id);
dbContact.Name = contact.Name;
UpdateWithUser(dbContact, user.UserId, "Contactrepo.UpdateContact");
return dbContact;
}
public ContactAttribute UpdateContactAttribute(ContactAttribute attribute, User user)
{
var dbAttribute = DbContext.ContactAttributes.Find(attribute.Id);
dbAttribute.AttributeValue = attribute.AttributeValue;
dbAttribute.Code = attribute.Code;
UpdateWithUser(dbAttribute, user.UserId, "ContactRepo.UpdateContactAttribute");
return dbAttribute;
}
Reply
Answers (
3
)
Object reference not set to an instance of an object
How to call second Action result in first Actionresult?