I'm beginer in ASP.NET MVC. I have a problem with models types in MVC controller. Here is my controller:
public async Task<IActionResult> DeleteAd(Guid id)
{
AdDTO ad = await _userAdService.GetAdDTOAsync(UserId,id);
return View(ad);
}
[HttpDelete]
public async Task<IActionResult> DeleteAd(RemoveUserAd command)
{
await DispatcheAsync<RemoveUserAd>(command);
return RedirectToAction("AllAds","UserAd");
}
Class 'AdDTO' has property 'Id', but 'RemoveUserAd' class has property named 'AdID'.
How can I pass AdDTO.Id to RemoveUserAd.AdId using mvc view? It is any way to bind models/properties in view in this section:
<form asp-action="DeleteAd">
<input type="hidden" asp-for="Id" />
<input type="submit" value="DeleteAd" class="btn btn-default" /> |
<a asp-action="Index">Back to List</a>
</form>
View code:
@model AdPortal.Infrastructure.DTO.AdDTO @{ ViewData["Title"] = "Delete"; } <h2>Details</h2> <div> <h4>Ad</h4> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Name) </dt> <dd> @Html.DisplayFor(model => model.Name) </dd> <dt> @Html.DisplayNameFor(model => model.Content) </dt> <dd> @Html.DisplayFor(model => model.Content) </dd> <dt> @Html.DisplayNameFor(model => model.AddDate) </dt> <dd> @Html.DisplayFor(model => model.AddDate) </dd> <dt> @Html.DisplayNameFor(model => model.ExpiryDate) </dt> <dd> @Html.DisplayFor(model => model.ExpiryDate) </dd> </dl> </div> <div> <form asp-action="DeleteAd"> <input type="hidden" asp-for="Id" /> <input type="submit" value="DeleteAd" class="btn btn-default" /> | <a asp-action="Index">Back to List</a> </form> </div>
AdDTO model
public class AdDTO
{
public Guid Id{get; set;}
public string Name {get;set;}
public string Content {get;set;}
public DateTime AddDate {get; set;}
public DateTime ExpiryDate {get; set;}
public string UserId {get; set;}
public Guid UserName {get;set;}
}
RemoveUserAd command model
public class RemoveUserAd
{
public Guid AdId {get; set;}
}
If AdDTO
is supposed to be the view model, then on your [HttpDelete]
action you don't take the RemoveUserAd
model. Instead, you take AdDTO
model back.
And then before you dispatch the command, you construct the command from the view model AdDTO
, either manually or using AutoMapper
.
[HttpDelete]
public async Task<IActionResult> DeleteAd(AdDTO model)
{
var command = new RemoveUserAd
{
AdId = model.Id,
....
};
await DispatcheAsync<RemoveUserAd>(command);
return RedirectToAction("AllAds","UserAd");
}
On your form, don't you need to specify method="delete"
?
On your Controller
, don't you need [ValidateAntiForgeryToken]
? Or you have it enable globally config.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
?
If you worry about OverPosting
, then in the action you just take Id
instead the whole AdDTO
model.
[HttpDelete]
public async Task<IActionResult> DeleteAd(Guid id)
{
var command = new RemoveUserAd
{
AdId = id,
....
};
await DispatcheAsync<RemoveUserAd>(command);
return RedirectToAction("AllAds","UserAd");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.