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

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();
}

Related

ASP.NET RC2 - ModelState doesn't validate elements of collection

Let's say that I have simple model with required attribute above property.
public class User
{
[Required]
string Name {get;set;}
string Surname {get;set;}
}
When I POST/PUT only one instance of User and Name is empty it works pretty well. ModelState is not valid and contains error.
When I POST/PUT collection of objects User and in some of them Name is empty then ModelState is valid and it does not contain any validation errors.
Could you tell me what is wrong with it and why it concerns only collections? I noticed same behaviour when I have one object with relation one-many. Then collection within this object also is not validated by ModelState.
I don't want to validate required fields manually, it should work automatically.
You need to create a ActionFilter
public class ModelStateValidActionFilter : IAsyncActionFilter
{
public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// Validate ICollection
if (context.ActionArguments.Count == 1 && context.ActionArguments.First().Value.GetType().IsListType())
{
foreach (var arg in (IList)context.ActionArguments.First().Value )
{
var parameters = arg.GetType().GetProperties();
foreach (var parameter in parameters)
{
var argument = context.ActionArguments.GetOrDefault(parameter.Name);
EvaluateValidationAttributes(parameter, argument, context.ModelState);
}
}
}
if (context.ModelState.IsValid)
{
return next();
}
context.Result = new BadRequestObjectResult(context.ModelState);
return Task.CompletedTask;
}
private void EvaluateValidationAttributes(PropertyInfo parameter, object argument, ModelStateDictionary modelState)
{
var validationAttributes = parameter.CustomAttributes;
foreach (var attributeData in validationAttributes)
{
var attributeInstance = parameter.GetCustomAttribute(attributeData.AttributeType);
var validationAttribute = attributeInstance as ValidationAttribute;
if (validationAttribute != null)
{
var isValid = validationAttribute.IsValid(argument);
if (!isValid)
{
modelState.AddModelError(parameter.Name, validationAttribute.FormatErrorMessage(parameter.Name));
}
}
}
}
and add it into your MVC options
services.AddMvc()
.AddMvcOptions(opts =>
{
opts.Filters.Add(new ModelStateValidActionFilter());
}

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();
}

Setting Up Multiple Actions

I was wondering if it was possible to have more than 1 action in the link. For example, If I wanted to have multiple links such as:
http://www.mywebsite.com/(CONTROLLER)/(ID)/(ACTION)
[http://]www.mywebsite.com/user/Micheal/EditMovies
[http://]www.mywebsite.com/user/Micheal/EditFavorites
Is there some sort of way to do this? If not, do I have to specify multiple id's in the function and then use a case to determine which page they are going to be sent to?
In my UserController.cs I have:
public ActionResult Index(string username)
{
if (username != null)
{
try
{
var userid = (Membership.GetUser(username, false).ProviderUserKey);
Users user = entity.User.Find(userid);
return View(user);
}
catch (Exception e)
{
}
}
return RedirectToAction("", "Home");
}
In my Routes I have:
routes.MapRoute(
name: "User",
url: "User/{username}",
defaults: new { controller = "User", action = "Index" }
);
What I'm trying to make it do is have additional functions for second actions so I can do something like:
User/{username}/{actionsAdditional}
And In my UserController I can put more actions which will leader to the second action actionsAdditional
public ActionResult Index(string username)
{
if (username != null)
{
try
{
var userid = (Membership.GetUser(username, false).ProviderUserKey);
Users user = entity.User.Find(userid);
return View(user);
}
catch (Exception e)
{
}
}
return RedirectToAction("", "Home");
}
public ActionResult EditFavorites()
{
//DoStuff
}
You could do this multiple ways, here's just one:
Set up a route to handle this:
routes.MapRoute("UserEditsThings",
"user/{id}/edit/{thingToEdit}",
new { controller = "UserController", action="Edit" },
new { thingToEdit = ValidThingsToEditConstraint() }
);
Then your action in the User Controller should look like this:
public ActionResult Edit(ThingToEdit thingToEdit) {
ThingToEditViewModel viewModel = new ThingToEditViewModel(thingToEdit);
return View(viewModel);
}
The RouteConstraint is what would take their input (the thingToEdit) and make sure it was valid (you could do this in a few places - like in a Custom ModelBinder):
public class ValidThingsToEditConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
//simplistic implementation simply to show what's possible.
return values['thingToEdit'] == "Favorites" || values['thingToEdit'] == "Movies";
}
}
Now, that way, you can have one method to Edit both Movies and Favorites, and you simply add a parameter to show what 'type' of thing they're editing.
If you wanted to keep your current route, you should be able to do the following:
routes.MapRoute("UserEditsThings",
"user/{id}/edit{thingToEdit}",
new { controller = "UserController", action="Edit" },
new { thingToEdit = ValidThingsToEditConstraint() }
);
I've been away from ASP.NET MVC for about 7 months, so this could be a little rusty. It has not been tested for syntax errors and bits of python may shine through. It should get you there, though.

ASP.NET MVC create a new database record

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?

Resources