如何通过一种方法访问多个视图

时间:2019-02-05 01:59:27

标签: razor asp.net-core tag-helpers

我添加了两个“用于编辑的视图”方法,第一个称为“编辑”,第二个称为“详细信息”,我想访问这两个视图中的每个视图,一个用于编辑,另一个用于显示细节...是否有一种方法确定索引视图内的Required View?我知道我们可以使用(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 个答案:

答案 0 :(得分:1)

如果我从一种方法中正确地理解它,则您希望根据某种逻辑将对象返回到2种不同的视图 因此,您只需使用return View(scientificEvent,"Your view name");