如何访问单个方法的多个视图
How Do I Access Multiple Views for A Single method
我添加了两个 Views For Edit 方法,第一个称为它(编辑),第二个称为它(详细信息)我想访问这两个视图中的每一个视图,一个用于编辑,另一个用于显示详细信息...是否有确定索引视图中所需视图的方法?我知道我们可以通过使用 (asp-action) 来确定动作是否有类似的东西来确定视图?
在控制器中编辑方法
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,EventNameAr,EventNameEn,EventType,CountryId,Venue,Date,SpecialityId,Organizers,ApplicationUserId,ContactDetails,IsFeatured,IsVisible,IsAccepted,Website,IsDecisionMaker,Description,EventDate,AridPrivileges,Image,ReportType")] ScientificEvent scientificEvent, IFormFile myfile)
{
if (id != scientificEvent.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
scientificEvent.ApplicationUserId = _userManager.GetUserId(User);
scientificEvent.Image = await UserFile.UploadeNewImageAsync(scientificEvent.Image,
myfile, _environment.WebRootPath, Properties.Resources.ScintificEvent, 50, 50);
_context.Update(scientificEvent);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ScientificEventExists(scientificEvent.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ApplicationUserId"] = new SelectList(_context.ApplicationUsers, "Id", "ArName", scientificEvent.ApplicationUserId);
ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "ArCountryName", scientificEvent.CountryId);
ViewData["SpecialityId"] = new SelectList(_context.Specialities, "Id", "ArSpecialityName", scientificEvent.SpecialityId);
return View(scientificEvent);
}
索引视图:我想确定要访问哪个视图的位置
@foreach (var item in Model)
{
@if (item.IsFeatured == true)
{
@if (item.Image != "")
{
<img class="img-rounded" src="@Url.Content("~/" + @ARID.Properties.Resources.ScintificEvent + "/" + item.Image)" width="380" height="200" />
}
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => item.EventNameAr)
</dt>
<dd>
<a asp-action="Edite" asp-route-id="@item.Id"> @Html.DisplayFor(modelItem => item.EventNameAr)</a>
</dd>
<dt>
@Html.DisplayNameFor(model => item.Speciality)
</dt>
<dd>
@Html.DisplayFor(model => item.Speciality.ArSpecialityName)
</dd>
<dt>
@Html.DisplayNameFor(model => item.ApplicationUser)
</dt>
<dd>
<a asp-action="Details" asp-controller="ApplicationUsers" asp-route-id="@item.ApplicationUser.Id">
@Html.DisplayFor(model => item.ApplicationUser.ArName)
</a>
<img class="img-rounded" src="@Url.Content("~/" + @ARID.Properties.Resources.ProfileImageFolder + "/" + item.ApplicationUser.ProfileImage)" width="50" height="50" /><br />
</dd>
<dt>
@Html.DisplayNameFor(model => item.EventType)
</dt>
<dd>
@Html.DisplayFor(model => item.EventType)
</dd>
</dl>
<hr style=" border-top:3px solid #275bad;">
}
}
如果我理解正确,从你想要的 1 种方法 return 对象到 2 种不同的视图,具体取决于某些逻辑
所以你可以简单地使用 return View(scientificEvent,"Your view name");
我添加了两个 Views For Edit 方法,第一个称为它(编辑),第二个称为它(详细信息)我想访问这两个视图中的每一个视图,一个用于编辑,另一个用于显示详细信息...是否有确定索引视图中所需视图的方法?我知道我们可以通过使用 (asp-action) 来确定动作是否有类似的东西来确定视图?
在控制器中编辑方法
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,EventNameAr,EventNameEn,EventType,CountryId,Venue,Date,SpecialityId,Organizers,ApplicationUserId,ContactDetails,IsFeatured,IsVisible,IsAccepted,Website,IsDecisionMaker,Description,EventDate,AridPrivileges,Image,ReportType")] ScientificEvent scientificEvent, IFormFile myfile)
{
if (id != scientificEvent.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
scientificEvent.ApplicationUserId = _userManager.GetUserId(User);
scientificEvent.Image = await UserFile.UploadeNewImageAsync(scientificEvent.Image,
myfile, _environment.WebRootPath, Properties.Resources.ScintificEvent, 50, 50);
_context.Update(scientificEvent);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ScientificEventExists(scientificEvent.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ApplicationUserId"] = new SelectList(_context.ApplicationUsers, "Id", "ArName", scientificEvent.ApplicationUserId);
ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "ArCountryName", scientificEvent.CountryId);
ViewData["SpecialityId"] = new SelectList(_context.Specialities, "Id", "ArSpecialityName", scientificEvent.SpecialityId);
return View(scientificEvent);
}
索引视图:我想确定要访问哪个视图的位置
@foreach (var item in Model)
{
@if (item.IsFeatured == true)
{
@if (item.Image != "")
{
<img class="img-rounded" src="@Url.Content("~/" + @ARID.Properties.Resources.ScintificEvent + "/" + item.Image)" width="380" height="200" />
}
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => item.EventNameAr)
</dt>
<dd>
<a asp-action="Edite" asp-route-id="@item.Id"> @Html.DisplayFor(modelItem => item.EventNameAr)</a>
</dd>
<dt>
@Html.DisplayNameFor(model => item.Speciality)
</dt>
<dd>
@Html.DisplayFor(model => item.Speciality.ArSpecialityName)
</dd>
<dt>
@Html.DisplayNameFor(model => item.ApplicationUser)
</dt>
<dd>
<a asp-action="Details" asp-controller="ApplicationUsers" asp-route-id="@item.ApplicationUser.Id">
@Html.DisplayFor(model => item.ApplicationUser.ArName)
</a>
<img class="img-rounded" src="@Url.Content("~/" + @ARID.Properties.Resources.ProfileImageFolder + "/" + item.ApplicationUser.ProfileImage)" width="50" height="50" /><br />
</dd>
<dt>
@Html.DisplayNameFor(model => item.EventType)
</dt>
<dd>
@Html.DisplayFor(model => item.EventType)
</dd>
</dl>
<hr style=" border-top:3px solid #275bad;">
}
}
如果我理解正确,从你想要的 1 种方法 return 对象到 2 种不同的视图,具体取决于某些逻辑
所以你可以简单地使用 return View(scientificEvent,"Your view name");