Kin
Kin

Reputation: 145

How to perform edit action in ASP.net Core?

How to perform edit action in ASP.net Core? I have the following code for product detail view action i need help on creating action for edit page

//IProduct interface

namespace Proj.Core.App.Common.Product
{    public interface IProductService
    {
        Task<IList<ProductDTO>> GetProducts();
    }
}

//Product Controller

 public class ProductController : Controller
    { 
    public IProductService ProductService { get; }
    public ProductController(IProductService ProductService)
        {
            ProductService = ProductService;
        }    
//DetailAction    
       [HttpGet()]
    public async Task<IActionResult> Detail(int id)
        {
            var ProductList= (await ProductService.GetProducts()).ToList();        
            var project = ProductList.FirstOrDefault(a => a.ID == id);
            @ViewBag.Product_Code = product.productCode;
            @ViewBag.Product_Name = product.productName;      

            return View();
        }
     }

how can i create action for Edit page action?

Upvotes: 1

Views: 1067

Answers (2)

Mathew Schlabaugh
Mathew Schlabaugh

Reputation: 181

I left a comment that the question really needs more information to answer properly. However, maybe the stuff below might help to get you started.

Here is an implementation using the methods you already have.

public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var productList = (await ProductService.GetProducts()).ToList();

    var product = productList.FirstOrDefault(a => a.ID == id);

    if (product == null)
    {
        return NotFound();
    }

    return View(product);
}

This is what typical entity framework implementation looks like. Extract what information you can from it.

public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var product = await _context.Products.FindAsync(id);

    if (product == null)
    {
        return NotFound();
    }

    return View(applications);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Product product)
{
    if (ModelState.IsValid)
    {
        try
        {
            _context.Update(product);

            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!ProductExists(product.Id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return RedirectToAction(nameof(Index));
    }

    return View(applications);
}

private bool ProductExists(int id)
{
    return _context.Products.Any(e => e.Id == id);
}

Hope that helps.

Happy coding!!!

Upvotes: 3

Mehrdad
Mehrdad

Reputation: 1733

    //DetailAction    
    [HttpPut()]
    public async Task<IActionResult> Put(Product model)
    {
        ..call your service or ...
        return View();
    }

Upvotes: 0

Related Questions