简体   繁体   English

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

[英]asp.net mvc getting data from textbox on view back to stored procedure to edit record

I am new to ASP.NET but have done 20 years of desktop dev.我是 ASP.NET 的新手,但已经做了 20 年的桌面开发。 I have a Customer class with fields CustId, CustName, CustNotes .我有一个Customer class 字段CustId, CustName, CustNotes

I have a view called CustView that has some input boxes that are to be pre-populated with customer details from a previous view using the CustID .我有一个名为CustView的视图,其中包含一些输入框,这些输入框将使用CustID预先填充上一个视图中的客户详细信息。

I can show the customer details in the text boxes but I cannot get the user edited textboxes (ie the user changes the name of a customer) back to a stored procedure in an Action.我可以在文本框中显示客户详细信息,但我无法将用户编辑的文本框(即用户更改客户名称)返回到操作中的存储过程。

I use a DB class called Cust1DBHandle to call the stored procedure.我使用名为 Cust1DBHandle 的 DB Cust1DBHandle来调用存储过程。 I have set up 3 buttons and 3 actions in a bid to get any of them to work, either by passing variables or using RequestString but nothing works.我已经设置了 3 个按钮和 3 个操作,以通过传递变量或使用RequestString来让它们中的任何一个工作,但没有任何效果。

  • Question #1 : how can I pass the new text values back as either global variables, variables in the action or using a datamodel?问题 #1 :如何将新文本值作为全局变量、操作中的变量或使用数据模型传回?

  • Question #2 : in the CustViewDBHandle , I populate a list of the results.问题 #2 :在CustViewDBHandle中,我填充了结果列表。 Is that the correct thing to do if it's only for one row of data?如果仅针对一行数据,这样做是否正确?

  • Question #3 : when pressing a SAVE button that executes a stored procedure, do you have to have a return in the action in the controller?问题 #3 :当按下执行存储过程的 SAVE 按钮时,controller 中的操作是否必须有返回?

Thanks.谢谢。

Cust.cs model Cust.cs model

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

There is a lot to parse here, but what strikes me as the main part of the problem you have noted is:这里有很多要解析的内容,但是您注意到的问题的主要部分让我印象深刻的是:

a) You methods on the controller are not all decorated with HttpPost attributes. a)您在 controller 上的方法并非都用 HttpPost 属性装饰。

b) The action on the form that you render will be looking for a POST endpoint with a name that matches the get b)您呈现的表单上的操作将查找名称与 get 匹配的 POST 端点

That said, you're going to find it hard to get answers for how to make this work, given that this is very non-idiomatic asp.net MVC code.就是说,鉴于这是非常不惯用的 asp.net MVC 代码,您会发现很难获得如何完成这项工作的答案。

I would strongly suggest working through a few tutorials, as web dev is considerably different to windows dev, with a different set of challenges.我强烈建议您完成一些教程,因为 web 开发与 windows 开发有很大不同,具有不同的挑战。 Hopefully your experience will let you skim through that quickly.希望您的经验能让您快速浏览。

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

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