This is the model UserPosts.cs:
public int Id { get; set; }
public string Destination { get; set; }
public DateTime DepartureDate { get; set; }
public string Meetplace { get; set; }
public byte NumberofSeats { get; set; }
public virtual ApplicationUser User { get; set; }
public string UserId { get; set; }
This is the Create method in the controller:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(UserPosts userposts)
{
var userId = User.Identity.GetUserId();
if (ModelState.IsValid)
{
userposts.UserId = userId;
_db.UserPosts.Add(userposts);
_db.SaveChanges();
return RedirectToAction("Index","UserPosts");
}
return View(userposts);
}
This is the Index view:
@model IEnumerable<Carpool.Models.UserPosts>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>All posts</h2>
<div class="allposts">
@foreach (var m in Model)
{
<div class="posts">
<table class="table">
<tr>
<td><b>Destination</b></td>
<td>@m.Destination</td>
</tr>
<tr>
<td><b>Departure Date</b></td>
<td>@m.DepartureDate</td>
</tr>
<tr>
<td><b>Meetplace</b></td>
<td>@m.Meetplace</td>
</tr>
<tr>
<td><b>Number of Seats</b></td>
<td>@m.NumberofSeats</td>
</tr>
<tr>
<td>
------>Here I want to add an actionlink/button<------
}
</td>
</tr>
</table>
</div>
<br /><br />
}
</div>
What I want to do is: when the user who is logged in click the actionlink or button that is displayed for every post I want to save that post in another view only for that user who clicked it and in that view I want to save all posts when the user click on the actionlink or button. When the user click on the actionlink or button I want NumberofSeats property to decrease with 1. Please help me, I am trying to do this for some days and I haven't found a solution yet. Thank you!