简体   繁体   中英

Render other controller method in razor view

As far as I know, a view is bound to a controller's action method like PatientController 's Details action method results are implemented in Patient->Details.cshtml inside the Views folder.

My question is how to call another controller action method like calling Appointment 's Index (in which I implemented a simple search Index(string ID)) inside Patient 's Details and how to render or display the results.

I tried this inside Details (Patient)

<form asp-controller="Appointments" asp-action="Index" asp-route-id="ID" method="get">
    <p>
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(m => m.ID)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => m.ID)
            </dd>
        </dl>
    </p>
</form>

But that is rendering Patient ID property.

Here is a demo:

ModelA:

public class ModelA
    {
        public string ID { get; set; }
    }

AController:

public IActionResult Index()
        {
            return View(new ModelA { ID="sampleID"});
        }

BController:

public IActionResult Index(string id)
        {
            return View();
        }

A/Index.cshtml:

<h1>ControllerA/Index</h1>
<form asp-controller="B" asp-action="Index" asp-route-id=@Model.ID method="get">
    <p>
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(m => m.ID)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.ID)
            </dd>
        </dl>
    </p>
    <input type="submit" value="submit"/>
</form>

B/Index.cshtml:

<h1>ControllerB/Index</h1>

result: 在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM