如何在不使用 entity framework 的情况下在 MVC4 中执行编辑功能?

How to perform edit function in MVC4 without using entity framework?

我只想使用 mvc4 编辑我的旧数据。例如,城市名称需要从 chennai 更改 (从模型填充的下拉列表)到 pune。谁能指导我吗?

下面是我的代码:

控制器:

[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='' where cityname='"+oldcityname+"'",conn))
            cmd.ExecuteNonQuery();



    }
         }

         return View(cg);
    }      

查看:

 @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>

创建包含新旧名称属性的视图模型

查看模型

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

控制器

[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
}

查看

@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" />
}

正如 Jon Skeet 指出的那样,使用参数化 SQL!