0

I just want to edit my old data using mvc4. For eg, the city name needs to be changed from chennai (dropdownlist which is populated from model) to pune. Can anyone guide me pls?

Below is my code:

Controller:

[HttpGet] public ActionResult display(Create model) { List<Create> city = new List<Create>(); using (connectionstring pcs = new connectionstring()) { city = pcs.grp.OrderBy(a => a.cityname).ToList(); } ViewBag.cityname = new SelectList(city, "cityname", "cityname"); return View(model); } [HttpPost, ActionName("display")] [ValidateAntiForgeryToken] public ActionResult display1( Create cg) { List<Create> city = new List<Create>(); using (connectionstring pcs = new connectionstring()) { city = pcs.grp.OrderBy(a => a.cityname).ToList(); } ViewBag.cityname = new SelectList(city, "cityname", "cityname"); if (ModelState.IsValid) { string oldgcityname = cg.cityname.ToString().Trim(); using (NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["portalconnectionstring"].ConnectionString)) { using( NpgsqlCommand cmd=new NpgsqlCommand("update tblcity set cityname='$1' where cityname='"+oldcityname+"'",conn)) cmd.ExecuteNonQuery(); } } return View(cg); } 

View:

 @using (Html.BeginForm()) { @Html.ValidationSummary(true) @Html.AntiForgeryToken() <table> <tr> <td> <div class="editor-label"> @Html.Label("Select old cityname") </div> </td> <td> <div class="editor-field"> @Html.DropDownListFor(model => model.cityname,@ViewBag.cityname as SelectList,"select") @Html.ValidationMessageFor(model => model.cityname) </div> </td></tr> <tr> <td> <div class="editor-label"> @Html.Label("Enter new cityname") </div> </td> <td> <div class="editor-field"> @Html.EditorFor(model => model.cityname) @Html.ValidationMessageFor(model => model.cityname) </div> </td></tr> <tr><td> <p> <input type="submit" value="Create" /> </p> </td></tr> 

6
  • What problem are you having? Commented Jan 20, 2015 at 6:38
  • You cant use DropDownListFor() with the model property and the ViewBag property having the same name. And then you have an EditorFor() that uses the same property name again (which will just be ignored on postback) Commented Jan 20, 2015 at 6:41
  • Yep. I don't know how to do this scenario?. Is there any other way to accomplish this task? Commented Jan 20, 2015 at 6:44
  • I need both drop down and new text box for the city name in the same view page. Commented Jan 20, 2015 at 6:45
  • 1
    Additionally, you should immediately stop building SQL like that, including values directly within the SQL. Use parameterized SQL, always. Otherwise, you have a potential SQL Injection attack. See bobby-tables.com Commented Jan 20, 2015 at 6:50

1 Answer 1

1

Create a view model that contains properties for the old and new names

View model

public class CreateVM { [Display(Name = "Old name")] [Required] public string OldName { get; set; } [Display(Name = "New name")] [Required] public string NewName { get; set; } public SelectList CityList { get; set; } } 

Controller

[HttpGet] public ActionResult Edit(CreateVM model) { CreateVM model = new CreateVM(); ... model.CityList = new SelectList(city, "cityname", "cityname"); return View(model); } [HttpPost] public ActionResult Edit(CreateVM model) { // the model now contains the selected old name and its new name } 

View

@model CreateVM @using(Html.BeginForm()) { @Html.LabelFor(m => m.OldName) @Html.DropDownListFor(m => m.OldName, Model.CityList, "-Please select-") @Html.ValidationMessageFor(m => m.OldName) @Html.LabelFor(m => m.NewName) @Html.TextBoxFor(m => m.NewName) @Html.ValidationMessageFor(m => m.NewName) <input type="submit" /> } 

And as Jon Skeet has noted, use parameterized SQL!

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Thanks for your response. I am receiving the below error while executing "{"\r\n(138,10) : error 3004: Problem in mapping fragments starting at line 138:No mapping specified for properties Create.citylist, Create.cityList in Set grp.\r\nAn Entity with Key (PK) will not round-trip when:\r\n Entity is type [mvcdemo.Models.Create]\r\n"}
@user3793029, That error is not related to the code in my answer. Its due to your code for updating the database. Debug you code to determine which line is causing the error. Google "error 3004: Problem in mapping fragments starting at line" to see if you can find a solution, and if not you need to ask a new question.
Hi, I got the solution. I just added new field called newcityname as you mentioned. But i don't want to add newcityname in my table. For now, i added and it works well. Thank you so much for your help.
You don't need to add the field in your table. I have shown you how to do it with a view model CreateVM which is not the same as your data model Create. You need to map the properties from you view model to the data model. See What is a view model in MVC
Its not entirely clear from your question, but I think all you are wanting to do is update an existing city name with a new name, in which case you just want something like UPDATE tblcity SET cityname = @NewName WHERE cityname = @OldName and pass the parameters from the view model

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.