简体   繁体   English

ASP.NET MVC控制器中的持久数据

[英]Persistent data in an ASP.NET MVC controller

I want to use the same view to insert and update data in a database. 我想使用相同的视图在数据库中插入和更新数据。

When I am in the "edit mode", I need to know what are the values before and after the edition (to know what has changed and perform operations based on that). 当我处于“编辑模式”时,我需要知道该版本前后的值是什么(要知道已更改的内容并根据该内容执行操作)。

I cannot store the "before edit" data in the model, as there are not mapped and I cannot store that data in the controller as a new instance is created between get and post. 我无法在模型中存储“编辑前”数据,因为没有映射,并且由于在get和post之间创建了新实例,因此无法将数据存储在控制器中。

For instance I have no other idea that using a static class or hidden fields but it doesn't sound to me to be a good practice. 例如,我没有其他想法使用静态类或隐藏字段,但是听起来不错。

How to achieve that in a proper way? 如何以适当的方式实现这一目标?

Thanks 谢谢

You can see the older data in 您可以在中查看较早的数据
var data = context.DEMO_MAST.Where(x => x.ID == model.ID).FirstOrDefault(); and new data in Model and just using If Condition you can do AddEdit both things in single method. 模型中的新数据,只需使用If Condition,就可以在单个方法中同时执行AddEdit

AddEdit Method AddEdit方法

 public int AddOrEdit(DEMO_MAST model)
            {
                try
                {
                    var data = context.DEMO_MAST.Where(x => x.ID == model.ID).FirstOrDefault();
                    if (data == null)
                    {
                        model.CREATED_ON = DateTime.Now;
                        this.context.DEMO_MAST.Add(model);
                    }
                    else
                    {
                        model.MODIFIED_ON = DateTime.Now;
                        this.context.Entry(data).CurrentValues.SetValues(model);
                    }
                    int flg = this.context.SaveChanges();
                    return flg;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }

Find

public DEMO_MAST Find(int ID)
{
  return context.DEMO_MAST.where(x=>x.ID==ID).FirstorDefault();
}

Controller 控制者

[HttpGet]
public ActionResult Index(int ID)
{
   DEMO_MAST model=new DEMO_MAST();
  if(ID>0)
  {
     model=Find(ID);   
  } 
return View(model);
}

[HttpPost]
public ActionResult Index(DEMO_MAST model)
{
  if(ModelState.IsValid)
  {
     int i = AddEdit(model); 
     if(i>0) 
     {
       ViewBag.Message="Record AddEdit Successfully";
     }
     else
     {
       ViewBag.Message="Error";
     }
  }
return View(model);
}

So when you run your application simple Index() view is display. 因此,当您运行应用程序时,将显示简单的Index()视图。

Index view for get data 索引视图以获取数据

   using (Html.BeginForm("Index", "Home"))
    {
        <div class="row">
            <div class="col-md-6 col-sm-3">
            </div>
            <div class="col-md-6 col-sm-9">
                <div id="sample_6_filter" class="dataTables_filter">
                    <label>
                        @Html.TextBox("ID", ViewBag.ID == null ? "" : (string)ViewBag.ID, new { @class = "form-control input-inline", placeholder = "ID" })
                        <button class="btn green-haze" type="submit" id="submit">
                            <i class="fa fa-search"></i>
                        </button>
                    </label>
                </div>
            </div>
        </div>
    }
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(x => x.ID)
        @Html.HiddenFor(x => x.CREATED_ON)
        <div class="form-horizontal">

            <div class="form-group">
                <label class="control-label col-sm-4">Name</label>
                <div class="col-sm-5">
                    @Html.TextBoxFor(a => a.Name, new { @class = "form-control", @maxlength = "50" })
                </div>
                <div class="col-sm-3 text-danger">

                </div>
            </div>
 <div class="form-group">
       <div class="col-sm-12 text-center">
                <button type="submit" name="command" value="createUser" class="btn btn-lg btn-green hvr-underline-from-center">Submit</button>
       </div>
  </div>
    }

You need to enter ID or any Uniq field of your database for get result in View. 您需要输入ID或数据库的任何Uniq字段才能在View中获得结果。 When you click on button the [HttpGet] Index method will be called and display records in view. 当您单击按钮时,将调用[HttpGet] Index方法并在视图中显示记录。

And after that you can modify your data.Click on submit so it will go for 然后您可以修改您的数据。单击提交,它将用于
'[HttPost] Index' remain things done automatically as per above code and methods. “ [HttPost]索引”仍然按照上述代码和方法自动完成。

Now you just need to set your data in view.. that's it. 现在,您只需要在视图中设置数据即可。就是这样。
Hope you understand!! 希望你能理解!! Enjoy!! 请享用!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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