ASP.NET MVC create a new database record - asp.net

I've been learning asp.net mvc 3. So, basically I'm trying to create a new record in database. However, I'm trying to keep a particular record predefined
public ActionResult Create()
{
var dc = new ServicesDataContext();
var model = new Maping();
var query = (from m in dc.Customers
where m.CustomerId == model.CustomerID
select m.CustomerId);
ViewData["CustomerID"] = query.First();
return View(model);
}
//
// POST: /Customerservice/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="CustomerServiceMappingID")] Maping serviceToCreate, FormCollection form)
{
if (!ModelState.IsValid)
return View();
var dc = new ServicesDataContext();
dc.Mapings.InsertOnSubmit(serviceToCreate);
try
{
dc.SubmitChanges();
}
catch (Exception e)
{
}
try
{
var id = Int32.Parse(form["CustomerID"]);
ViewData["CustomerID"] = id;
return RedirectToAction("Index", new { id = id });
}
catch
{
return RedirectToAction("INDEX", "Home");
}
}
So this is what I did. So, the case is that id value in second action method is what i needed. However the second method gets redirectd to index so viewdata value is lost.And the thing i did in the first crate method is wrong because no value is assigned. So, can u please help me with this problem.

Use TempData, not ViewData - it will be valid until it is read again. Also why are you adding it into viewdata when it is being passed as a parameter in the RedirectToAction?

Related

entity-framework error: An object with the same key already exists in the ObjectStateManager

I got thiserror
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same
key.
only because this line
oldProject = db.Projectes.Find(project.ID);
I need it in order to get the object before the edit, and afterwards compare the values changes between before and after the edit, I do it in the third if statement (that compare one values), and in the "SFSiteEmailSend.ProjectEdited" function that only check the changes and send email about it.
By the way, I never change the "oldproject", the error still exist even if I delete all the email funcation and the third(last) if statement.
// POST: /Project/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Project project)
{
if (ModelState.IsValid)
{
// For Email Send - start
string UrlProject = Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("Detail", "Project", new { id = project.ID });
Project oldProject = new Project();
Project newProject = new Project();
newProject = project;
oldProject = db.Projectes.Find(project.ID);
SFSiteEmailSend.ProjectEdited(oldProject, newProject, UrlProject);
// For Email Send - end
if (oldProject.Finished == false && newProject.Finished == true)
{
project.DateFinished = DateTime.Now;
}
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
Your code is full of redundancy. Strip some of it out and it should become easier to spot the error:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Project newProject)
{
if (ModelState.IsValid)
{
string UrlProject = Request.Url.GetLeftPart(UriPartial.Authority)
+ Url.Action("Detail", "Project", new { id = newProject.ID });
//both oldProject and newProject have the same Key but only oldProject
//is attached
Project oldProject = db.Projectes.Find(newProject.ID);
SFSiteEmailSend.ProjectEdited(oldProject, newProject, UrlProject);
if (!oldProject.IsFinished && newProject.IsFinished )
newProject.DateFinished = DateTime.Now;
//now you try to attach another project with the same Key.
//It will fail when you call SaveChanges
db.Entry(newProject).State = EntityState.Modified;
//This is an option:
d.Entry(oldProject).State = EntityState.Detached;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(newProject);
}
or you could load oldProject with AsNoTracking in the first place - as you already found out
try
project.entitykey = oldproject.entitykey before savechanges
I found a solution
Instead
public ActionResult Edit(Project project)
{
Project beforeEditProject = db.Projectes.Find(project.ID);
....
}
}
Use
public ActionResult Edit(Project project)
{
Project beforeEditProject = db.Projectes.AsNoTracking().First(p => p.ID == project.ID);
.....
}

Save data instead of adding to database

I'm trying to edit an article in my asp.net mvc project. This is what I do when I create a project:
public ActionResult Create(ArticleViewModel model)
{
if (ModelState.IsValid)
{
try
{
// Get the userID who created the article
User usr = userrepo.FindByUsername(User.Identity.Name);
model.UsernameID = usr.user_id;
repository.AddArticle(model.Title, model.Description, model.ArticleBody);
}
catch (ArgumentException ae)
{
ModelState.AddModelError("", ae.Message);
}
return RedirectToAction("Index");
}
return View(model);
}
In my repository:
public void AddArticle(string Title, string Description, string ArticleBody)
{
item Item = new item()
{
item_title = Title,
item_description = Description,
article_body = ArticleBody,
item_createddate = DateTime.Now,
item_approved = false,
user_id = 1,
district_id = 2,
link = "",
type = GetType("Article")
};
try
{
AddItem(Item);
}
catch (ArgumentException ae)
{
throw ae;
}
catch (Exception)
{
throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
"If the problem persists, please contact your system administrator.");
}
Save();
// Immediately persist the User data
}
public void AddItem(item item)
{
entities.items.Add(item);
}
But now I want to edit an article, this is what I have till now:
public ActionResult Edit(int id)
{
var model = repository.GetArticleDetails(id);
return View(model.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ArticleViewModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the User
try
{
item Item = repository.GetArticleDetailsByTitle(model.Title);
Item.item_title = model.Title;
Item.item_description = model.Description;
Item.article_body = model.ArticleBody.
// HERE I NEED TO SAVE THE NEW DATA
return RedirectToAction("Index", "Home");
}
catch (ArgumentException ae)
{
ModelState.AddModelError("", ae.Message);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
As you can see I check the adjusted text and drop it in "Item". But how can I save this in my database? (the function in my repository)
I think your save() method had entityobject.SaveChanches()
So you want to call that save() method in here
try
{
item Item = repository.GetArticleDetailsByTitle(model.Title);
Item.item_title = model.Title;
Item.item_description = model.Description;
Item.article_body = model.ArticleBody.
**Save();**
return RedirectToAction("Index", "Home");
}
should be need to only Save() method, could not need to AddItem() method .
I'm afraid you'll need to get article again from database, update it and save changes. Entity framework automatically tracks changes to entites so in your repository should be:
public void EditArticle(Item article)
{
var dbArticle = entities.items.FirstOrDefault(x => x.Id == article.Id);
dbArticle.item_title = article.item_title;
//and so on
//this is what you call at the end
entities.SaveChanges();
}

How can I convert linq object to asp.net object..?

Code:
Domain ob = new Domain();
[HttpPost]
public ActionResult Create(Domain ob)
{
try
{
//// TODO: Add insert logic here
FirstTestDataContext db = new FirstTestDataContext();
tblSample ord = new tblSample();
ord = ob;
db.tblSamples.InsertOnSubmit(ord);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Here I am getting an error like this
Cannot implicitly convert type 'mvcInsertLinqForms.Models.Domain' to 'mvcInsertLinqForms.tblSample'
You cannot assign ord to ob because they are not of the same type. You seem to be attempting to map the view model (ob) to your domain model (tblSample). You could do this by setting the corresponding properties of the domain model:
[HttpPost]
public ActionResult Create(Domain ob)
{
try
{
tblSample ord = new tblSample();
// now map the domain model properties from the
// view model properties which is passed as action
// argument:
ord.Prop1 = ob.Prop1;
ord.Prop2 = ob.Prop2;
...
FirstTestDataContext db = new FirstTestDataContext();
db.tblSamples.InsertOnSubmit(ord);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
and to avoid doing this mapping manually you could use a tool such as AutoMapper which could help you mapping back and forth between your view models and your domain models.
[HttpPost]
public ActionResult (Domain model) // or (FormCollection form), use form.get("phone")
{
//---
return View();
}

asp.net mvc create a database record

So, I've been trying to create a record.however I created it successfully but the problem is that I may need the ID that has been auto incremented.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="CustomerServiceMappingID")] Maping serviceToCreate, FormCollection form)
{
if (!ModelState.IsValid)
return View();
var dc = new ServicesDataContext();
dc.Mapings.InsertOnSubmit(serviceToCreate);
try
{
dc.SubmitChanges();
}
catch (Exception e)
{
}
after this, I tried to do this which has not been working
var id = Int32.Parse(form["CustomerServiceMappingID"]);
var qw = (from m in dc.Mapings
where id == m.CustomerServiceMappingID
select m.CustomerID).First();
// var id = Int32.Parse(form["CustomerID"]);
return RedirectToAction("Index", new { id = qw });
Now I need to send Customer ID as a parameter to Index.. SO, can u help me out..
Thanks,
I would rewrite as (dont exclude the ID from the parameter list - a particular reason this needs to be excluded?):
[HttpPost]
public ActionResult Create(Maping serviceToCreate)
{
if (!ModelState.IsValid)
{
return View();
}
var dc = new ServicesDataContext();
dc.Mapings.InsertOnSubmit(serviceToCreate);
dc.SubmitChanges();
//try to get the values from 'Maping' model if possible?
var qw = (from m in dc.Mapings
where m.CustomerServiceMappingID == serviceToCreate.CustomerServiceMappingId
select m.CustomerID).First();
return RedirectToAction("Index", new { id = qw });

ASP .net Dropdown list not saving the selected value to database

In the following code i want to save the value selected by user from drop downlist into database. but whatever value is selected by user, first value of dropdown lsit is saved to database
View
<% =Html.DropDownList("lstUsertype", (SelectList)ViewData["UserTypeID"])%>
Controller
public ActionResult CreateUser()
{
UmUser _UmUser = new UmUser();
UMRepository _UMRepository = new UMRepository();
EvoLetDataContext db = new EvoLetDataContext();
ViewData["UserTypeID"] = new SelectList(_UMRepository.FillUserTypes(), "UserTypeID", "UserType",2);
return View(_UmUser);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateUser(UmUser _umUser)
{
//try
//{
if (ModelState.IsValid)
{
//try
//{
UserRepository _UserRepository = new UserRepository();
_UserRepository.Add(_umUser);
_UserRepository.Save();
return RedirectToAction("Details", new { id = _umUser.UserID });
/*}
catch
{
ModelState.AddModelErrors(_umUser.GetRuleViolations());
}*/
}
return View();
//}
/*catch
{
return View();
}*/
}
This is how I'm doing it successfully:
<%= Html.DropDownListFor(model => model.Value, new SelectList(Values, "Key", "Value", Model.Value), "[select]")%>
Where Values is of type IDictionary and Value is of type Guid
Hope this helps!

Resources