asp.net mvc 从视图中的文本框获取数据返回存储过程以编辑记录

Posted

技术标签:

【中文标题】asp.net mvc 从视图中的文本框获取数据返回存储过程以编辑记录【英文标题】:asp.net mvc getting data from textbox on view back to stored procedure to edit record 【发布时间】:2022-01-15 19:11:06 【问题描述】:

我是 ASP.NET 的新手,但已经做了 20 年的桌面开发。我有一个Customer 类,字段为CustId, CustName, CustNotes

我有一个名为CustView 的视图,其中包含一些输入框,这些输入框将使用CustID 预先填充上一个视图中的客户详细信息。

我可以在文本框中显示客户详细信息,但无法将用户编辑的文本框(即用户更改客户名称)返回到操作中的存储过程。

我使用名为Cust1DBHandle 的DB 类来调用存储过程。我设置了 3 个按钮和 3 个操作,以通过传递变量或使用 RequestString 使它们中的任何一个工作,但没有任何作用。

问题 #1:如何将新文本值作为全局变量、操作中的变量或使用数据模型传回?

问题 #2:在CustViewDBHandle 中,我填充了结果列表。如果只针对一行数据,这样做是否正确?

问题#3:当按下执行存储过程的SAVE按钮时,控制器中的动作是否必须有return?

谢谢。

Cust.cs 模型

public partial class Cust

    [DisplayName("Cust ID")]
    public Int32 CustID  get; set; 
    [DisplayName("Customer Name")]
    // [Required(ErrorMessage = "This field is required")]
    public string CustName  get; set; 
    
    [DisplayName("Customer Notes")]
    public string CustNotes  get; set; 

    public string ErrorMessageCust  get; set; 

CustView.cshtml:

@model IEnumerable<App22.Models.Cust>

@
    ViewBag.Title = "CustView";


<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<header>
</header>

<meta name="viewport" content="width=device-width" />

<title>CustViewy</title>

<html>
<head>
</head>
<style>
th, td 
    padding: 5px;

</style>
<body>
<p>

</p>
<p>CUSTOMER DETAILS ARE HERE</p>
<form name="1" method="post">
    <fieldset>
        <legend>Headline Details</legend>
        <table>
            @foreach (var item in Model)
            
                <tr>
                    <td>
                        <label for="CustID">CustID:</label>
                    </td>
                    <td>
                        <input type="text" name="1CustID" value="@Html.DisplayFor(modelItem => 
item.CustID)" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="CustName">CustName:</label>
                    </td>
                    <td>
                        <input type="text" name="2CustName" value="@Html.DisplayFor(modelItem 
 => item.CustName)" />
                    </td>
                </tr>
                <td>
                    <label for="CustNotes">Cust Notes:</label>
                </td>
                <td>
                    <input type="text" name="3CustNotes" value="@Html.DisplayFor(modelItem => 
item.CustNotes)" />
                </td>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" name="action:Save1" value="Save" />

                        <button style="background-color:red" type="button" name="tree0" 
class="btn btn-primary" onclick="location.href='@Url.Action("SaveCust0","Cust1")'">
                            SAVE0 &raquo;
                        </button>

                        <button style="background-color:blue" type="button" name="tree1" 
class="btn btn-primary" onclick="location.href='@Url.Action("SaveCust1","Cust1",new  CustyIDSave = item.CustID , CustyNameSave = item.CustName )'">
                            SAVE1 &raquo;
                        </button>

                        <button style="background-color:green" type="button" name="tree2" class="btn btn-primary" onclick="location.href='@Url.Action("SaveCust2","Cust1")'">
                            SAVE2 &raquo;

                        </button>
                    </td>
                    <td>
                    </td>
                </tr>
            
        </table>
    </fieldset>
</form>
</body>
</html>

Cust1Controller.cs:

public class Cust1Controller : Controller

    public ActionResult SaveCust0()
    
        string message = "";
        
        message = Request.Form["CustName"].ToString();
        return Content(message);

        CustViewDBHandle dbhandle = new CustViewDBHandle();
        ModelState.Clear();
        dbhandle.SaveCust(Convert.ToInt32(Request.Form["CustID"]), 
Request.Form["CustName"].ToString());
    

    public ActionResult SaveCust1(int CustyIDSave, string CustyNameSave)
    
        CustViewDBHandle dbhandle = new CustViewDBHandle();
        ModelState.Clear();
        dbhandle.SaveCust(CustyIDSave, CustyNameSave);
        return null;
    

    [HttpPost]
    public ActionResult SaveCust2(int CustyIDSave, string CustyNameSave)
    
        CustViewDBHandle dbhandle = new CustViewDBHandle();
        ModelState.Clear();
        dbhandle.SaveCust(CustyIDSave, CustyNameSave);
        return null;
    

    // GET: Cust1
    public ActionResult Index()
    
        Cust1DBHandle dbhandle = new Cust1DBHandle();
        ModelState.Clear();
        return View("~/Views/Cust/Cust1.cshtml",dbhandle.GetCust(""));
        
       // return View("~/Views/Cust/Cust1.cshtml"); //This works
    

    [HttpGet]
    public ActionResult Reload(string tree)
    
        //tree = "Breaker2";
        Cust1DBHandle dbhandle = new Cust1DBHandle();
        ModelState.Clear();
        return View("~/Views/Cust/Cust1.cshtml", dbhandle.GetCust(tree));
        //Cust1DBHandle dbhandle = new Cust1DBHandle();
        //ModelState.Clear();
        //return View("~/Views/Cust/Cust1.cshtml", dbhandle.GetCust(SearchBy));

        // return View("~/Views/Cust/Cust1.cshtml"); //This works
    

    public ActionResult ViewCust(int CustyIDView)
    
        //tree = "Breaker2";
       
        CustViewDBHandle dbhandle = new CustViewDBHandle();
        ModelState.Clear();
        return View("~/Views/Cust/CustView.cshtml", dbhandle.GetCust(CustyIDView));
        //Cust1DBHandle dbhandle = new Cust1DBHandle();
        //ModelState.Clear();
        //return View("~/Views/Cust/Cust1.cshtml", dbhandle.GetCust(SearchBy));

        // return View("~/Views/Cust/Cust1.cshtml"); //This works
    

CustViewDBHandle.cs:

public class CustViewDBHandle

    // ********** VIEW CUSTOMER DETAILS ********************
    public List<Cust> GetCust(int CustyID)
    
        GlobalVar.connection();
        List<Cust> CustyChosen = new List<Cust>();

        SqlCommand cmd = new SqlCommand("psv_CustView1", GlobalVar.con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CustyID", CustyID);

        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();

        GlobalVar.con.Open();
        sd.Fill(dt);
        GlobalVar.con.Close();

        foreach (DataRow dr in dt.Rows)
        
            CustyChosen.Add(
                new Cust
                
                    CustID = Convert.ToInt32(dr["CustID"]),
                    CustName = Convert.ToString(dr["CustName"]),
                    CustNotes = Convert.ToString(dr["CustNotes"]),
                );
            GlobalVar.GlobCustName1 = Convert.ToString(dr["CustName"]); //This method uses 
Global Var to get data to pass to form.  Can pass anything that way.
        

        return CustyChosen;
    

    public int SaveCust(int CustyID, string CustyName)
    
        GlobalVar.connection();
        SqlCommand cmd = new SqlCommand("psu_CustSave1", GlobalVar.con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CustyID", CustyID);
        cmd.Parameters.AddWithValue("@CustyName", CustyName);

        GlobalVar.con.Open();
        cmd.ExecuteNonQuery();

        //SqlDataAdapter sd = new SqlDataAdapter(cmd);
        //DataTable dt = new DataTable();

        //sd.Fill(dt);
        GlobalVar.con.Close();

        return 1;
    

【问题讨论】:

【参考方案1】:

这里有很多东西要解析,但让我印象深刻的是你注意到的问题的主要部分是:

a) 你在控制器上的方法并不都是用 HttpPost 属性修饰的。

b) 您呈现的表单上的操作将查找名称与 get 匹配的 POST 端点

也就是说,鉴于这是非常不习惯的 asp.net MVC 代码,您会发现很难找到如何完成这项工作的答案。

我强烈建议学习一些教程,因为 Web 开发与 Windows 开发有很大不同,面临着不同的挑战。希望您的经验能让您快速浏览一遍。

【讨论】:

以上是关于asp.net mvc 从视图中的文本框获取数据返回存储过程以编辑记录的主要内容,如果未能解决你的问题,请参考以下文章

asp.net mvc c# - 是不是可以从 CodeBehind 中的模型访问视图中文本框的值?

显示从 ASP.NET MVC 中的命令行进程获取的数据

Asp.net 从 asp 中的文本框获取值到代码后面

使用 MVC 从视图中的隐藏文本框中获取数据

无法更改 ASP.NET MVC 3 中文本框的值

如何在 ASP.NET MVC 的视图中调用多个操作?