I'm developing a web application, using ASP.Net
I have two models,
the first, Users:
public class Users
{
[Key]
[DisplayName("__ Users UserID __")]
public int UserID { get; set; }
[StringLength(7)]
[DisplayName("User LAN ID")]
public ICollection<WorkView>? WorkView { get; set; }
}
The second, WorkView:
public class WorkView
{
[Key]
[DisplayName("** WorkView ID **")]
public int ID { get; set; }
[DisplayName("** WorkView mainUserID **")]
public int? mainUserID{ get; set; }
public Users? Users { get; set; }
public string? FirstName { get; internal set; }
public string? LastName { get; set; }
public string? UserName { get; set; }
public string? JobTitle { get; set; }
public string? OfficeLocation { get; set; }
public string? ManagerFirstName { get; set; }
public string? ManagerFamilyName { get; set; }
public string? Email { get; set; }
}
That is the Controller:
[HttpGet("WorkView/{id}")]
//Opening the WorkView Creation
public async Task<IActionResult> WorkViewCreate(int? id)
{
if (id == null || _context.Users == null)
{
return View();
}
var user = await _context.Users.FindAsync(id);
if (user == null)
{
return NotFound();
}
var workView = (from w in _context.WorkView
join u in _context.Users on w.mainUserID equals u.UserID
where (w.mainUserID == id)
select new WorkView
{
ID = w.ID,
mainUserID = u.UserID,
}).FirstOrDefault();
var workViewTest = _context.WorkView;
Console.WriteLine("workView.ID = " + workView.ID);
Console.WriteLine("workView.mainUserID = " + workView.mainUserID);
return View(workView);
}
and eventually my view:
<script src="~/js/workViewScript.js" defer>
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<form asp-controller="Home" asp-action="WorkViewedit" asp-route-id="@Model?.OfficeLocation">
<div style="border:ridge;background-color:lightsalmon;">
<label asp-for="mainUserID"></label>
<input asp-for="mainUserID" type="text" class="form-control" aria-label="mainUserID" aria-describedby="basic-mainUserID" ;" style="width:auto;" />
</div>
<div style="border:ridge;background-color:lightsalmon;">
<label asp-for="ID"></label>
<input asp-for="ID" type="text" class="form-control" aria-label="ID" aria-describedby="basic-ID" ;" style="width:auto;" />
</div>
<div style="border:ridge;background-color:lightsalmon;">
<label asp-for="mainUserID"></label>
<input asp-for="mainUserID" type="text" class="form-control" aria-label="mainUserID" aria-describedby="basic-mainUserID" ;" style="width:auto;" />
</div>
<input type="submit" value="submit" />
</form>
My problem, is when my View is opening, I have the Users ID used in it, instead of the WorkView ID...
I don't see what causes that, no matter what I'm changing in the models, the view or the controller, I'm only getting the same ID, so I can't save anything from my view, as the ID is not the correct one.
I'm running a "check" in the controller, and I do have the correct ID's
workView.ID = 472
workView.mainUserID = 23
but once I'm returning the View, the workView.ID returning the mainUserID, which is the Users one. And it's not correct.
I don't know what I'm doing wrong, but I would really appreciate any idea.
Cheers
If this could help, I fixed my issue by using
ModelState.Clear();
in my controller before returning the View.