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>