none
.net MVC中关于Html.BeginForm()的一些疑问? RRS feed

  • 问题

  • 这边的功能就是修改商品模型的数据然后更新到数据库。 先上部分代码

    //商品的model类
    public class Product
        {
            [HiddenInput(DisplayValue =false)]
            public int ProductID { get; set; }
            [Required(ErrorMessage ="请输入产品名称")]
            public string Name { get; set; }
            [DataType(DataType.MultilineText)]
            [Required(ErrorMessage = "请输入Description")]
            public string Description { get; set; }
            [Required]
            [Range(0.01,double.MaxValue,ErrorMessage ="输入一个正确的价格")]
            public decimal Price { get; set; }
            [Required(ErrorMessage = "请输入Category")]
            public string Category { get; set; }
        }


    //CRUD的管理 这里只实现了修改
    public class AdminController : Controller
        {
            private IProductsRepository repository;
            public AdminController(IProductsRepository repo)
            {
                repository = repo;
            }
            // GET: Admin
            public ActionResult Index()
            {
                return View(repository.Products);
            }
    
            public ActionResult Edit(int productid)
            {
                Product product = repository.Products.FirstOrDefault(p => p.ProductID == productid);
                return View(product);
            }
    
            [HttpPost]
            public ActionResult Edit(Product product)
            {
                if (ModelState.IsValid)
                {
                    repository.SavePorduct(product);
                    TempData["message"] = string.Format("{0}已被保存", product.Name);
                    return RedirectToAction("Index");
                }
                else
                    return View(product);
            }
        }
    public class EFProductRepository : IProductsRepository
        {
            private EFDbContext context = new EFDbContext();
    
            public IEnumerable<Product> Products => context.Products;
    
            //保存模型数据的修改
            //如果ID等于0就添加否则修改
            public void SavePorduct(Product product)
            {
                if(product.ProductID==0)
                {
                    context.Products.Add(product);
                }
                else
                {
                    Product dbEntry = context.Products.Find(product.ProductID);
                    if(dbEntry!=null)
                    {
                        dbEntry.Name = product.Name;
                        dbEntry.Description = product.Description;
                        dbEntry.Price = product.Price;
                        dbEntry.Category = product.Category;
                    }
                }
                context.SaveChanges();
            }
        }
    @model Store.Domain.Entities.Product
    @{
        ViewBag.Title = "Edit";
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
    }
    <div class="panel">
        <div class="panel-heading">
            <h3>Edit @Model.Name  @Model.ProductID</h3>
        </div>
    
        @using (Html.BeginForm("Edit","Admin"))
        {
            <div class="panel-body">
                @foreach (var property in ViewData.ModelMetadata.Properties)
                {
                    if (property.PropertyName != "ProductID")
                    {
                        <div class="form-group">
                            <label>@(property.DisplayName ?? property.PropertyName)</label>
                            @if (property.PropertyName == "Description")
                            {
                                @Html.TextArea(property.PropertyName, null,
                                    new { @class = "form-control", rows = 5 })
                            }
                            else
                            {
                                @Html.TextBox(property.PropertyName, null,
                                    new { @class = "form-control" })
                            }
                            @Html.ValidationMessage(property.PropertyName)
                        </div>
                    }
                }
            </div>
            <div class="panel-footer">
                <input type="submit" value="保存" class="btn btn-primary" />
                @Html.ActionLink("取消并返回", "Index", null, new {
               @class = "btn btn-default"
           })
            </div>
        }
    </div>


    我发现在Edit.cshtml中使用@using (Html.BeginForm("Edit","Admin"))方法时会出现自动把商品的ID修改为0 然后导致修改的失败。

    而使用@using (Html.BeginForm())时则不会改变商品的ID,然后修改成功。


    这2个方法为什么会导致不同的结果哪?

    如果使用@using (Html.BeginForm("Edit","Admin"))应该怎样去更改?

    请各位大佬指教

    2019年8月28日 4:29

全部回复

  • Hi whiskets,

    有关MVC的问题,我建议直接到ASP.NET Forum提问,那里会提供更加专业的回答。

    感谢你的理解!

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月28日 5:23