Can a Developer help me with this?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 年 前
I need to List, Create, and Edit products in the main website itself : Nop.Web (not Nop.Admin).

I used List, Create, and Edit Scaffolding Views with the Product model "@model Nop.Core.Domain.Catalog.Product"
List and Create both work.

But having trouble with Edit. The Edit screen displays the form with the fields I have chosen.
But when I click "Save" it doesn't update any of the fields:

        public ActionResult Edit(int id)
        {
            var product = _productService.GetProductById(id);
            return View(product);
        }

        [HttpPost]
        public ActionResult Edit(Product model)
        {
            model.AdminComment = "test";      
            _productService.UpdateProduct(model);   // doesn't work

            return RedirectToAction("Index");
        }
View:
@model Nop.Core.Domain.Catalog.Product
@using (Html.BeginForm())
{
        @Html.HiddenFor(model => model.Id)

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
         ........
}

I also tried : _productRepository.Update(model); but that didn't work either.

The "_productService.InsertProduct(model)" works but "_productService.UpdateProduct(model)" doesn't.

In the Nop.Admin Edit code, it looks like this :  public ActionResult Edit(ProductModel model, bool continueEditing)  { . .
But the ProductModel is in the Admin project and I can't access it.
But the Create works with just the Product model.

Can anyone see why the Edit is not updating?
Thanks
10 年 前
Set breakpoint into your [HttpPost] public ActionResult Edit(Product model) method.
When you hit the breakpoint, do you see filled model?
Do you see any errors in log?
10 年 前
Are you using Ajax?  Check that no client side errors are being thrown. (Turn on browser developer tools)
10 年 前
Mariann wrote:
When you hit the breakpoint, do you see filled model?Do you see any errors in log?

Yes, the Product gets populated, it just doesn't update. Nothing in the logs.
[quote]Check that no client side errors are being thrown. (Turn on browser developer tools) [/quote]
Not sure how to use that tool or even what to look for at this point.

But I did get it working. It seems the _productService.UpdateProduct(product) wants a ProductModel view model to work.
The only access to that model is in the Nop.Admin project.

So what I did, which is kind of crazy, is create a Controller in the Admin project, have it update the ProductModel,
and return back to the Controller in Nop.Web. Looks something like this :

namespace Nop.Admin.Controllers
{
    public class GiftListEditController : Controller
    {
        private readonly IProductService _productService;
        public GiftListEditController(IProductService _productService)  {
             this._productService = _productService;      }


public ActionResult Edit(int id)  // created using Edit scaffold
        {
            var product = _productService.GetProductById(id);
            var model = product.ToModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ProductModel model)
        {
            var product = _productService.GetProductById(model.Id);
            if (ModelState.IsValid)
            {
                product = model.ToEntity(product);
                product.UpdatedOnUtc = DateTime.UtcNow;
                _productService.UpdateProduct(product);
            }
            return RedirectToAction("Index", "GiftList", new { area = "" }); // returns to Nop.Web
        }
VIEW :
@model Nop.Admin.Models.Catalog.ProductModel

@{
   Layout = "~/Nop.Web/Views/Shared/_ColumnsOne.cshtml";  } << this actually worked; getting a View from Nop.Web

@using (Html.BeginForm("Edit", "GiftListEdit"))
..........
@Html.ActionLink("Back to List", "Index", "GiftList", new { area = "" }, null)  << returns to Nop.Web


For the Edit link to go to the Nop.Admin Controller :

@Html.ActionLink("Edit", "Edit", "GiftListEdit", new { id = item.Id }, new { Area = "Admin" } )  


This is crazy but it works.
10 年 前
Was playing around and it turns out I don't need the crazy Nop.Admin controller code (above).

Got this to work by mapping the fields of the model to the retrieved product :


        [HttpPost]
        public ActionResult Edit(Product model)
        {
            var product = _productService.GetProductById(model.Id);
            // map each property in product to form fields in the Product model parameter :
            product.AdminComment = model.AdminComment;
            product.ShortDescription = model.ShortDescription;
            product.Name = model.Name;
            ...............
            _productService.UpdateProduct(product);
            ........
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.