-1

I am struggling to duplicate data from one column to the next. I want foodId and MenuItemId to be the same. Is there a way to add a code in the Controller to have both columns to have the same value? This is how it looks like currently:

FoodId   | MenuItemId | Rating
---------+------------+------
1        | NULL       |   5
1        | NULL       |   4
2        | NULL       |   5
1        | NULL       |   5

I would like my end result to look like this:

FoodId   | MenuItemId | Rating
---------+------------+------
  1      |     1      |   5
  1      |     1      |   4
  2      |     2      |   5
  1      |     1      |   5

This is my controller:

public IActionResult Create()
{
    var listOfNumbers = new List<int>() { 1, 2, 3, 4, 5 };
    var listOfRatings = listOfNumbers.Select(x => new { Id = x, Value = x.ToString() });

    ViewData["Rating"] = new SelectList(listOfRatings, "Id", "Value");
    ViewData["FoodId"] = new SelectList(_context.MenuItems, "Id", "Name");

    return View();
}
    
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Title,FoodReview,Rating,Date,FoodId,MenuItemId,UserId")] Reviews reviews)
{
    if (ModelState.IsValid)
    {
        var userId = _userManager.GetUserId(HttpContext.User);

        reviews.UserId = userId;
        reviews.Date = DateTime.Now;

        _context.Add(reviews);

        await _context.SaveChangesAsync();

        return RedirectToAction("Index");
    }

    var listOfNumbers = new List<int>() { 1, 2, 3, 4, 5 };
    var listOfRatings = listOfNumbers.Select(x => new { Id = x, Value = x.ToString() });

    ViewData["Rating"] = new SelectList(listOfRatings, "Id", "Value", reviews.Rating);
    ViewData["FoodId"] = new SelectList(_context.MenuItems, "Id", "Name", reviews.FoodId);

    return View(reviews);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

you can link these properties using getter setter

public class Review
{
public int Id {get; set;}

,,,,, another properties

public int MenuItemId
{
get {return FoodId}
set { FoodId=value}
}

public int FoodId {get; set;}

}

or on the contrary , what you like more

Serge
  • 40,935
  • 4
  • 18
  • 45