نوشته شده توسط
mmbguide
البته شما درست میفرمایید ولی مورد سوال بنده نمایش این ها نیست.. و داخل لینک هم آیتم ها رو از دیتابیس نگرفته این مقداری مسئله رو برای من پیچیده میکنه چون دقیقا نقطه ای که با ثبت آیتم انتخابی در دراپ دان به مشکل میخورم همین جا هست که از دیتابیس میخونمش.
بک و ویوی کدم رو ضمیمه میکنم و پایینش سوالم رو دوباره روشن میکنم.
Edit.cdhtml.cs:
namespace BookStore.Pages.CRUD_CategoryTypes
{
public class EditModel : PageModel
{
private readonly ApplicationDbContext _context;
public EditModel (ApplicationDbContext context)
{
_context = context;
}
[BindProperty] public CategoryType CategoryType { get; set; }
[BindProperty] public string SelectedCategoryType { get; set; }
public SelectList Categories { get; set; }
public async Task<IActionResult> OnGetAsync (int? id)
{
if (id == null)
{
return NotFound ();
}
CategoryType = await _context.CategoryTypes.FirstOrDefaultAsync (m => m.Id == id);
if (CategoryType == null)
{
return NotFound ();
}
var productCategory = _context.CategoryTypes.Find (CategoryType.Id);
Categories = new SelectList (_context.CategoryTypes, "Name", "Id", productCategory.Name);
return Page ();
}
public async Task<IActionResult> OnPostAsync ()
{
if (!ModelState.IsValid)
{
return Page ();
}
_context.Attach (CategoryType).State = EntityState.Modified;
var categoryInDb = _context.CategoryTypes.FirstOrDefault (c => c.Id == CategoryType.Id);
if (categoryInDb == null)
{
return NotFound ();
}
categoryInDb.Name = CategoryType.Name;
var productCategory = await _context.CategoryTypes.FirstOrDefaultAsync (u => u.Id == CategoryType.Id);
if (SelectedCategoryType != productCategory.Name)
{
_context.Remove(CategoryType).State = EntityState.Deleted;
_context.CategoryTypes.AddAsync(Id) = EntityState.Added;
}
return RedirectToPage ("./Index");
}
}
}
در کد بالا داخل OnPost میخوام وقتی عنوان جدیدی در دراپ دان انتخاب کرد بعد از زدن ثبت به این شکل باشه که عنوان قبلی برای این محصول حذف بشه و عنوان جدید جای گزینش بشه.
کد زیر هم ویوی کار هست .
Edit.cshtml:
کد HTML:
<form method="post"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="CategoryType.Id" /> <div class="form-group"> <select asp-for="SelectedCategoryType" asp-items="Model.Categories" class="form-control"></select> </div> <div class="form-group"> <input type="submit" value="ذخیر تغیرات" class="btn btn-warning" /> <i class="fa fa-save"></i> </div> </form>