简体   繁体   English

试图了解如何提交表格

[英]Trying to understand how to submit a form

I'm currently working on an "Activity Report" application on a first year internship, where employees can select a specific project and assign how many hours they have worked on it per day. 我目前正在第一年的实习中研究“活动报告”应用程序,在该应用程序中,员工可以选择一个特定的项目,并指定每天要处理多少小时。

The selection of projects (depending on the employees id) and the display of the page seem to be working. 项目的选择(取决于雇员的ID)和页面的显示似乎正在起作用。 But here's my problem : once I click on the displayed image that is supposed to submit the form - it seems to reload the page as if nothing happened ! 但是,这是我的问题:单击显示的图像后,该图像应该提交表单-似乎重新加载了页面,好像什么都没发生!

The View line of the submission is as following : 提交的“查看”行如下:

<% using (Html.BeginForm("ActivityInputCreate", "Project"))
       { %>
[...]

<td>
    <input type="image" style="border: 0; width: 16px;" src="../../Content/Black_16x16/Save.png" onclick="this.form.submit" alt="save" />
    <input type="image" style="border: 0; width: 16px;" src="../../Content/Black_16x16/Cancel.png" onclick="    window.location.href = '/Project/ActivityInputIndex'; return false;" alt="cancel" /></td>
         <!-- the first line being to save and the second to cancel -->
<td>
}

I don't know how to handle a form submission but here's the controller (the first of the same name being the non [HttpPost] one : 我不知道如何处理表单提交,但这是控制器(同名的是非[HttpPost]一个):

[HttpPost]
        public ActionResult ActivityInputCreate(Project objToCreate, FormCollection form)
        {
            string year = form["Years"];
            int iyear = int.Parse(year);
            string month = form["Months"];
            int imonth = int.Parse(month);
            string strUser = Environment.UserName.ToLower();

            CalendarController c = new CalendarController();
            ViewBag.ListYears = c.ListYears(iyear);
            ViewBag.ListMonths = c.ListMonths(imonth);
            ViewBag.ListDaysOfMonth = _service.ListDaysOfMonth(iyear.ToString(), imonth.ToString());

            //nea11
            Session["ActivityInputYear"] = iyear.ToString();
            Session["ActivityInputMonth"] = imonth.ToString();

            ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));
            ViewBag.ProjectList = pr.ListProject();

            List<IEnumerable<SelectListItem>> ddlists = new List<IEnumerable<SelectListItem>>();

            foreach (string s in ViewBag.ListDaysOfMonth)
                ddlists.Add(_service.ListHours(0)); //id de UserActivity à la place de 0

            ViewBag.ddlists = ddlists;

            //nea12
            string strProject = form["Project"];

            //nea13
            string strSep = ";";
            string strDatas = "";
            int l = 0;
            foreach (var jour in ViewBag.ListDaysOfMonth)
            {
                string nom = "Hours" + l.ToString();
                strDatas += form[nom] + strSep;
                l++;
            }

            //ajout dans la base
            UserActivityDb db = new UserActivityDb(@"metadata=res://*/Models.CRA.csdl|res://*/Models.CRA.ssdl|res://*/Models.CRA.msl;provider=Microsoft OLE DB Provider for SQL server;provider connection string=&quot;data source=(local)\SQLEXPRESS;initial catalog=CRAV34;persist security info=True;user id=sa;password=as0;multipleactiveresultsets=True;App=EntityFramework&quot;");
            if (null != strProject)
                db.setFormattedData(iyear, imonth, ViewBag.ListDaysOfMonth, int.Parse(strProject), strUser, strDatas, true);

            IEnumerable<Project> lp = _service.List(strUser, iyear.ToString(), imonth.ToString());
            lp = lp.Where(p => (true == db.ExistUserActivity(iyear, imonth, ViewBag.ListDaysOfMonth, p.ProjectId, strUser)));


            //nea35
            List<string[]> lstInts = new List<string[]>();

            foreach (Project p in lp)
            {
                string strInts = db.getFormattedData(iyear, imonth, ViewBag.ListDaysOfMonth, p.ProjectId, strUser, null, 0);
                string[] ints = strInts.Split(';');

                lstInts.Add(ints);

            }
            ViewBag.ProjectInts = lstInts;

            return View("ActivityInputIndex", lp);
        }

I'd love to give you more precision but please keep in mind that most of the code isn't mine and is the last interns'. 我希望为您提供更多的精确度,但是请记住,大多数代码不是我的,而是最后的实习生。

TL;DR : Display of the page is fine, I want to submit once the DropDownList are filled, submission button doesn't do anything. TL; DR:页面的显示很好,我想在DropDownList填满后提交,提交按钮没有任何作用。

Thank you. 谢谢。

Turns out the issue came with the input of my username (through the SQL INSERT). 原来问题出在我的用户名输入中(通过SQL INSERT)。

  string strUser = User.Identity.Name;

Is used with : 用于:

command.Parameters.Add("@Login", SqlDbType.VarChar, 50).Value = sLogin;

And works just fine. 并且工作正常。 The issue was not with the sending of a form (which it did receive) but the way the SQL request was built. 问题不在于发送表单(它确实接收到),而在于构建SQL请求的方式。

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

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