简体   繁体   中英

How to update the data in database using linq

I have learning C# MVC using linq. Now I don't know how to save the updated data in database

How to update the data?

public ActionResult Edit(Table_2 emp)
{
    List<Table_2> objemp = new List<Table_2>();
    using (var dbs = new guruEntities())
    {
        var name = emp.name;
        var dep = emp.dprmt;
        var query = from n in dbs.Table_2.Where(n => n.name == name && n.dprmt == dep)
            orderby n.id
            select n;
        dbs.SaveChanges();
    }

    return RedirectToAction("Index");
}

View:

<div class="form-horizontal">
<h4>Table_2</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
<div class="form-group">
    @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.dprmt, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.dprmt, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.dprmt, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</div>
</div>

I don't know how to get the user data from textbox and to get saved in the database

As you said you just want to update records in database. then first of all get records from the database which you want to update records like below

public ActionResult Edit(Table_2 emp)
{
      var table2 = _dbContext.Table_2.Where(x => x.Id == emp.id).FirstOrDefault(); //SingleOrDefault() also used.
      if (table2 == null)
      {
           throw new Exception("not found");
      }
       table2.name = emp.name;
       table2.dprmt = emp.dep;
       dbs.SaveChanges();
       return RedirectToAction("Index");
}

first get records from database and update that records and call SaveChanges() . and if you need to add new records into database then create first entity like

Table_2 entity = new Table_2();
        entity.id = 1;
        entity.name = "name";
        entity.dprmt = "dept";
        _dbContext.Table_2.Add(entity)
        dbs.SaveChanges();

you can save and update into database. let me know if any questions :)

you are using the "query" variable to retrieve data from a data store (I assume Entity Framework). however, once you fetch the data, you need to update it. it should be something like

query.name = "new name";

and then when you call save changes, EF will pick up the change and update the DB.

PS:

the vars you created "name" and "deps" are redundant. you can update the entity directly once you get it from the context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM