How do I pre-populate data of a model to form I am getting data in model, I can see in quickwatch, but it's not populating in form.
This is my model and view. I am also sending data from this model it's working fine there, I am really newbie to ASP.NET MVC:
namespace UserMaster.Models
{
public class UserMasterClass
{
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public string MobileNo { get; set; }
public string Domain { get; set; }
public string WinUserId { get; set; }
public bool IsActive { get; set; }
}
public class UserMasterSaveClass
{
public UserMasterClass UserGetData {get; set;}
public string Password { get; set; }
public string IPAddress { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public int ModifiedBy { get; set; }
public DateTime ModifiedOn { get; set; }
}
public class UsersViewModel
{
public List<UserMasterClass> UserMasterData { get; set; }
public UserMasterSaveClass NewUserData { get; set; }
}
}
Partial view is
@model UsersViewModel
<!-- Insert new users -->
@await Html.PartialAsync("Index", Model.NewUserData)
<!-- Display existing users -->
@await Html.PartialAsync("UsersData", Model.UserMasterData)
And final view is
@{
ViewData["Title"] = "Home Page";
@model UserMasterSaveClass
}
<div class="form-group">
<label for="UserName">UserName</label>
@Html.TextBoxFor(m => m.UserGetData.UserName, new { @class = "form-control", id = "UserName" })
</div>
->pre-populate data of a model to form I am getting data in model
Do you mean that you want to have the form to contain origin data when it's rendering? If so, my sample below might help you. In the parent page, we use the viewmodel which containing the required form data. And when use use html helper to render the partial view, we passed the data which is the entity of the viewmodel at the same time.
---- Index.cshtml ----
@model UsersViewModel;
@await Html.PartialAsync("_Index", Model.NewUserData)
@await Html.PartialAsync("_UsersData", Model.UserMasterData)
---- Controller: ----
public IActionResult Index()
{
var model = new UsersViewModel
{
UserMasterData = new List<UserMasterClass> {
new UserMasterClass {
UserId = 1,
UserName = "user1",
},
new UserMasterClass {
UserId = 2,
UserName = "user2",
}
},
NewUserData = new UserMasterSaveClass
{
Password = "password",
IPAddress = "192.168.1.1",
UserGetData = new UserMasterClass
{
UserId = 1,
UserName = "user1",
}
}
};
return View(model);
}
---- Partial view _UsersData.cshtml ----
@model IEnumerable<UserMasterClass>;
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
</tr>
}
</tbody>
</table>
---- Partial View _Index.cshtml ----
@model UserMasterSaveClass;
<form asp-action="New">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserGetData.UserId" class="control-label"></label>
<input asp-for="UserGetData.UserId" class="form-control" />
<span asp-validation-for="UserGetData.UserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="UserGetData.UserName" class="control-label"></label>
<input asp-for="UserGetData.UserName" class="form-control" />
<span asp-validation-for="UserGetData.UserName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="IPAddress" class="control-label"></label>
<input asp-for="IPAddress" class="form-control" />
<span asp-validation-for="IPAddress" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>