我正在尝试提交销售自行车的注册输入详细信息。当我单击“提交”按钮时,表单不会提交并保留在当前页面

问题描述 投票:0回答:1

BicycleController 中的操作

        public ActionResult SellerAddPage()
        {
            var model = new BikeModel();
            return View(model);
        }
        [HttpPost]
        public ActionResult SellerAddPage(BikeModel bicycle, HttpPostedFileBase Image)
        {
                try
                {
                    // Save image to folder
                    if (Image != null && Image.ContentLength > 0)
                    {
                        var fileName =    Path.GetFileName(Image.FileName);
                        var path =   Path.Combine(Server.MapPath("~/Content/Image"), fileName);
                        Image.SaveAs(path);
                        bicycle.Image = "/Content/Images/" + fileName; // Save the relative path in the database
                    }

                    int? userId = Session["UserId"] as int?;
                    if (userId == null)
                    {
                        // Handle case where UserId is not in session (user not logged in or session expired)
                        ViewBag.ErrorMessage = "User is not logged in.";
                        return View(bicycle);
                    }
                    else
                    {
                        bicycle.UserID = userId.Value;
                        // Save bicycle details to the database
                        using (var connection = new SqlConnection(connectionString))
                        {
                            string query = "INSERT INTO [sales].[bicycle] (user_id,brand_name, category_name, description, picture) VALUES (@user_id,@brand_name, @category_name, @description, @picture)";
                            SqlCommand cmd = new SqlCommand(query, connection);
                            cmd.Parameters.AddWithValue("@user_id", bicycle.UserID);
                            cmd.Parameters.AddWithValue("@brand_name", bicycle.Brand);
                            cmd.Parameters.AddWithValue("@category_name", bicycle.Category);
                            cmd.Parameters.AddWithValue("@description", bicycle.Description);
                            cmd.Parameters.AddWithValue("@picture", bicycle.Image);

                            connection.Open();
                            cmd.ExecuteNonQuery();
                        }
                    ViewBag.Message = "Bicycle uploaded successfully.";
                    return RedirectToAction("SellerPage");

                }
            }
                catch (Exception ex)
                {
                    ViewBag.ErrorMessage = "An error occurred while adding the bicycle: " + ex.Message;
                }
            return View("SellerPage");
        }



Model


    public class BikeModel
    {
        [Required]
        public int UserID { get; set; }
        public int BicycleID { get; set; }
        [Required]
        public string Brand { get; set; }
        public List<SelectListItem> ListBrand { get; set; }
        [Required]
        public string Category { get; set; }
        public List<SelectListItem> ListCategory {  get; set; }
        [Required]
        public string Description { get; set; }
        [Required]
        public string Image { get; set; }
        public BikeModel()
        {
            ListBrand = new List<SelectListItem>
            {
                new SelectListItem{Value="Electra",Text="Electra"},
                new SelectListItem{Value="Haro",Text="Haro"},
                new SelectListItem{Value="Heller", Text = "Heller"},
                new SelectListItem{Value="Pure Cycles", Text = "Pure Cycles"},
                new SelectListItem{Value="Ritchey", Text = "Ritchey"},
                new SelectListItem{Value="Strider", Text = "Strider"},
                new SelectListItem{Value="Sun Bicycles", Text = "Sun Bicycles"},
                new SelectListItem{Value="Surly", Text = "Surly"},
                new SelectListItem{Value="Trek", Text = "Trek"},

            };
            ListCategory = new List<SelectListItem>
            {
                new SelectListItem{Value="Children Bicycles", Text = "Children Bicycles"},
                new SelectListItem{Value="Comfort Bicycles", Text = "Comfort Bicycles"},
                new SelectListItem{Value="Cruisers Bicycles", Text = "Cruisers Bicycles"},
                new SelectListItem{Value="Cyclocross Bicycles", Text = "Cyclocross Bicycles"},
                new SelectListItem{Value="Electric Bikes", Text = "Electric Bikes"},
                new SelectListItem{Value="Mountain Bikes", Text = "Mountain Bikes"},
                new SelectListItem{Value="Road Bikes", Text = "Road Bikes"},
            };

        }```

    here is my
查看 @model u23704137_HW02.Models.BikeModel

@{

ViewBag.Title = "卖家添加页面"; }

                                   @using (Html.BeginForm("SellerAddPage", "Bicycle", FormMethod.Post, new { enctype =                           "multipart/form-data" }))

{ @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>BicycleModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Brand, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Brand, new { htmlAttributes = new { @class = "form-control",@id="brandId" } })
            @Html.ValidationMessageFor(model => model.Brand, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class =    "form-control" } })
            @Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-  danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-  danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label     col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(m => m.Image,new {htmlAttributes=new { @class = "form-control" }    })
            @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
        </div>
    </div>

     <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create"  formmethod="post" class="btn btn-default" />
        </div>
    </div>
    </div>

}``

我尝试过使用很多东西,包括 get 方法,但我无法提交表单,大多数解决方案都与 asp.net core 有关,而我在 .net 框架上工作。我还尝试重新创建项目并在 http post 方法中添加异常处理。

c# asp.net asp.net-mvc model-view-controller frameworks
1个回答
0
投票

我建议您将

SellerAddPage
POST 方法重命名为某个名称(例如
SellerAddPageAddBicycle
),因为它与返回您的视图的方法名称相同,这可能会导致您的问题。

我强烈建议您在浏览器选项卡中查看开发人员工具以了解控制台错误,并在网络选项卡中查看对服务器发出的请求(如果有)。

顺便说一句,不要使用 GET 请求来修改数据状态,POST,以及 PATCHPUT 适用于更改状态的方法。

© www.soinside.com 2019 - 2024. All rights reserved.