如何在asp.net mvc

时间:2017-09-22 16:20:42

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

我在单个视图中有2个表单,下面是代码。

 @model MCRP.Models.tblRequest
@{
  ViewBag.Title = "Review Request";
}
@using (Html.BeginForm("InsertForm", "Home"))
{
    @Html.EditorFor(model => model.ContactEmailAddress, new { htmlAttributes = new {id = "txtdelivery"} })
    <input type="submit" onclick="return validate();" value="submit" id="submitfilteration" class="btn btn-primary pull-left" />
}
@using (Html.BeginForm("Delete", "Home"))
{
     Are you sure you want to delete Property ?
    <input type="submit" value="Delete" class="btn btn-danger" />
}

但是点击删除按钮,插入txtdelivery的文本就会消失

控制器

public ActionResult ReviewRequest()
{         

        if (Session["AddtoCart"] != null)
        {
            DataTable dt = (DataTable)Session["AddtoCart"];
            if (dt.Rows.Count > 0)
            {
                ViewBag.addedtocart = (DataTable)Session["AddtoCart"];
            }
            else
            {
                ViewBag.addedtocart = null;
                ViewBag.EmptyCart = "Your Cart Is empty";
            }
        }
        else
        {
            ViewBag.addedtocart = null;
            ViewBag.EmptyCart = "Your Cart Is empty";
        }
        return View();

}
public ActionResult InsertForm(tblRequest Request)
{
       int requestid = 0;
        int RequestProperty = 0;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("InsUpsDelTbl_Request"))
            {
                try
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    cmd.Parameters.AddWithValue("@Operation", "InsertRequest");
                    cmd.Parameters.AddWithValue("@AgencyName", Request.AgencyName);
                    cmd.Parameters.AddWithValue("@Contact", Request.Contact);
                    cmd.Parameters.AddWithValue("@DeliveryAddress", Request.DeliveryAddress);
                    cmd.Parameters.AddWithValue("@ContactPhone", Request.ContactPhone);
                    cmd.Parameters.AddWithValue("@ContactEmailAddress", Request.ContactEmailAddress);
                    cmd.Parameters.AddWithValue("@Notes", Request.Notes);
                    requestid = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();

                }
                catch (Exception ex)
                { throw ex; }
            }
            using (SqlCommand cmd = new SqlCommand("InsUpsDelTbl_Request"))
            {
                DataTable dt = new DataTable();
                dt = (DataTable)Session["AddtoCart"];
                try
                {
                    if (dt.Rows.Count > 0)
                    {
                        cmd.Connection = con;
                        cmd.CommandType = CommandType.StoredProcedure;
                        con.Open();
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@Operation", "InsertRequestProperty");
                            cmd.Parameters.AddWithValue("@RequestID", requestid);
                            cmd.Parameters.AddWithValue("@PropertyID", dt.Rows[i]["PropertyId"].ToString());
                            cmd.Parameters.AddWithValue("@Quantity", dt.Rows[i]["Quantity"].ToString());
                            RequestProperty= Convert.ToInt32(cmd.ExecuteScalar());
                        }
                        con.Close();
                        if (requestid > 0 && RequestProperty>0)
                        {
                            TempData["InsertRequestProperty"] = ConfigurationManager.AppSettings["RequestAdded"].ToString().Replace("{@requestid}", Convert.ToString(requestid));
                            Session["AddtoCart"] = null;
                        }
                    }
                }
                catch (Exception ex)
                { throw ex; }
            }
        }
        return RedirectToAction("ReviewRequest");
}
public ActionResult Delete(FormCollection property)
{            
 string PropertyID = "";
        foreach (var key in property.AllKeys)
        {
            PropertyID = property["hddeleteGroupid"];
        }

        DataTable dt = new DataTable();
        if (Session["AddtoCart"] != null)
        {
            dt = (DataTable)Session["AddtoCart"];
            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                DataRow dr = dt.Rows[i];
                if (dr[0].ToString() == PropertyID)
                {
                    dt.Rows.Remove(dr);
                }
            }
            Session["AddtoCart"] = dt;
        }
        return RedirectToAction("ReviewRequest");

}

由于我是MVC的新手,我不知道请帮帮我

更多细节我的页面更多细节如图

所示

enter image description here

现在点击删除一个弹出窗口,删除对话框打开,按下删除按钮,购物车中添加的一个项目被删除,文本框中插入的文本也会消失,

enter image description here

0 个答案:

没有答案