I'm new to ASP.NET MVC and have a little question.
I have a user model like this:
public class UserDetailsViewModels : ViewBaseModels
{
[Display(Name = "Benutzername")]
public string Username { get; set; }
[Display(Name = "E-Mail")]
public string Email { get; set; }
[Display(Name = "Anmerkung")]
public string Note { get; set; }
}
In my view I have this HTML markup:
<div class="form-group">
@Html.LabelFor(x => x.Username)
@Html.TextBoxFor(x => x.Username, new { @class = "form-control input-sm" })
</div>
<!-- /.form-group -->
<div class="form-group">
@Html.LabelFor(x => x.Email)
@Html.TextBoxFor(x => x.Email, new { @class = "form-control input-sm" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.Note)
@Html.TextBoxFor(x => x.Note, new { @class = "form-control input-sm" })
</div>
<!-- /.form-group -->
I have a jQuery Ajax post in the document.ready, to call the controller. My controller looks like this:
[HttpPost]
public async Task<JsonResult> GetUser(UserDetailsViewModels m, int id)
{
try
{
var dtsource = await RequestManager.DoGet<User>("User/" + id); //Call the API
m.Email = dtsource.Email;
m.Note = dtsource.Note;
m.Username = dtsource.Username;
return Json(m);
}
catch (Exception ex)
{
return Json(new { error = ex.Message });
}
}
I know that I can bind the results to the text boxes via jQuery. My question is now, can I pass the result directly to the model. Unfortunately, I could not find the right one using Google
return PartialView("UserDetailPartial", m)and provide the markup in UserDetailParital.cshtml. Your ajax success would be$("target").html(result)where result is the html fragment from "UserDetailPartial".