试图了解如何提交表单

时间:2015-09-07 09:01:42

标签: c# asp.net asp.net-mvc

我目前正在制作一份"活动报告"在第一年的实习期间申请,员工可以选择一个特定的项目,并分配他们每天工作的小时数。

项目的选择(取决于员工ID)和页面的显示似乎正在起作用。但这是我的问题:一旦我点击显示的图像应该提交表单 - 它似乎重新加载页面,好像什么也没发生!

提交的视图行如下:

<% 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>
}

我不知道如何处理表单提交,但这里是控制器(同名的第一个是非[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);
        }

我很乐意为您提供更高的精确度,但请记住,大部分代码都不是我的,并且是最后的实习生。

TL; DR:显示页面很好,我想在DropDownList填充后提交,提交按钮不做任何事情。

谢谢。

1 个答案:

答案 0 :(得分:0)

结果输入我的用户名(通过SQL INSERT)来解决问题。

  string strUser = User.Identity.Name;

用于:

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

工作得很好。问题不在于发送表单(它确实收到),而在于SQL请求的构建方式。