编辑-在索引视图中保存而无需进入编辑视图

时间:2020-09-01 14:50:01

标签: c# asp.net-mvc model-view-controller

我有一个MVC-C#应用程序。它只有很少的页面具有由脚手架向导生成的CreateEditDelete功能。一切正常。我需要执行其他操作,该操作无法自动生成。

那是

  1. 我有一个带有实体框架的数据模型和一个为此的视图模型
  2. 该索引的所有必要数据均可用并显示为html表
  3. 我将表的1列更改为@EditorFor,以便可以更改值。

我想要的是一种将视图的内容以及我更改过的数据可编辑控件中传递给控制器​​的方法。 我不希望转到其他“编辑视图” 并在此处执行。

我尝试了以下操作:

  • 放置@using (Html.BeginForm())并将保存按钮设为提交按钮

  • 使用操作链接按钮传递模型值,如下所示,并在控制器中Edit method中进行更改

              <a href="@Url.Action("Edit","InvoicePayments",
                      new {
                          PaymentId = item.PaymentId,
                          InvoiceId = item.InvoiceId,
    

请让我知道我需要做什么才能完成这项工作。

2 个答案:

答案 0 :(得分:0)

https://dotnetfiddle.net/XDj2lW这是一个.net小提琴

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
    
    public class SampleViewModel
    {
        public SampleViewModel()
        {
            this.FieldOne = "1";
            this.FieldTwo = "2";
            this.FieldThree = "3";
        }
        public string FieldOne { get; set; }
        public string FieldTwo { get; set; }
        public string FieldThree { get;set; }
    }
}
using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorldMvcApp
{
    
    public class SampleViewModel
    {
        public SampleViewModel()
        {
            this.FieldOne = "1";
            this.FieldTwo = "2";
            this.FieldThree = "3";
        }
        public string FieldOne { get; set; }
        public string FieldTwo { get; set; }
        public string FieldThree { get;set; }
    }
}

@model HelloWorldMvcApp.SampleViewModel
@{
    Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>

        <!-- CSS Includes -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    </head>
    
    <body>
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
            
    
                @using (Html.BeginForm())
                {
                    <div class="form-group">
                        @Html.LabelFor(m => m.FieldOne)
                        @Model.FieldOne
                        @Html.LabelFor(m => m.FieldTwo)
                        @Model.FieldTwo
                        @Html.LabelFor(m => m.FieldThree)
                        @Model.FieldThree
                        @Html.TextBoxFor(model => model.FieldThree, new {@class="form-control"}) 
                    </div>
                
                    <button type="button" class="btn btn-success submit">Edit</button>
                }
    
                <br/><br/>
                <div class="alert alert-warning fade">
                    <img src="http://entechprod.blob.core.windows.net/dotnetfiddle/morpheus.jpg" style="max-width:100%;"/><br/><br/>
                    <strong><span class="alert-content"></span></strong>
                </div>
            </div>
        </div>

        <!-- JS includes -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
        
    </body>
</html>

净小提琴的内容 基本上,您可以使用强类型模型作为索引,并将值作为经过修改的值的该模型的一个实例传递给控制器​​

答案 1 :(得分:0)

模型

public class SampleViewModel
{
    public SampleViewModel()
    {
        
    }
    public SampleViewModel(string f1, string f2, string f3 )
    {
        this.FieldOne = f1;
        this.FieldTwo = f2;
        this.FieldThree = f3;
    }
    public string FieldOne { get; set; }
    public string FieldTwo { get; set; }
    public string FieldThree { get;set; }
}
public class MasterViewModel
{
    public MasterViewModel()
    {
        this.svmLst = new List<SampleViewModel>();

        SampleViewModel svm1 = new SampleViewModel("a", "b", "c");
        SampleViewModel svm2 = new SampleViewModel("p", "", "r");
        SampleViewModel svm3 = new SampleViewModel("z", "y", "z");

        svmLst.Add(svm1);
        svmLst.Add(svm2);
        svmLst.Add(svm3);
    }
    public List<SampleViewModel> svmLst { get; set; }
}

控制器

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
    

        return View(new HelloWorldMvcApp.MasterViewModel());
    }
    [HttpPost]
    public ActionResult Index(HelloWorldMvcApp.MasterViewModel model)
    {   
        //your value is stored here and sent back to controller in model container
        //you can see the value you edited in return page FieldTwoColum
        
        
        return View(model);
    }
}

视图

@model HelloWorldMvcApp.MasterViewModel
@{
    Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>

        <!-- CSS Includes -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    </head>
    
    <body>
    
    
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.svmLst[0].FieldOne)
            </th>

            <th>
                @Html.DisplayNameFor(model => model.svmLst[0].FieldTwo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.svmLst[0].FieldThree)
            </th>

            <th></th>
        </tr>
      
        @using (Html.BeginForm())
        {
          for(int i=0;i<Model.svmLst.Count();i++)
          {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => Model.svmLst[i].FieldOne)
                    @Html.HiddenFor(modelItem => Model.svmLst[i].FieldOne)
                </td>
                <td>
                    @Html.EditorFor(modelItem =>  Model.svmLst[i].FieldTwo)
                </td>
                <td>
                    @Html.DisplayFor(modelItem =>  Model.svmLst[i].FieldThree)
                    @Html.HiddenFor(modelItem =>  Model.svmLst[i].FieldThree)
                </td>
                <td>
                    <input type="submit" class="btn btn-success" value="Edit"/>
                </td>
            </tr>
        
    }
}
    </table>
    
            
    

        <!-- JS includes -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
        
    </body>
</html>