Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

c# - Form not redirecting to the action in cshtml

I have a .cshtml file that shows a page of my application. In the application, there is a top menu where there are different buttons. The Code is:-

<div id="top_menu">
    <form action="@Url.Action("Pause", new { sessionId = Model, schoolId = ViewData["SchoolId"] })" method="post">
        <button type="submit">Pause</button>
    </form>

    <button id="score_button" type="button" class="text-group">
        <span>Score:</span>
        <span style="font-size: 1.5em" data-bind="text: score"></span>
    </button>

    <span id="elapsed_time" class="text-group">
        <span>Time elapsed:</span>
        <span style="font-size: 1.5em" data-bind="text: time"></span>
    </span>

    <button type="button" onclick="SR.ERSIM.showInfo('info_resources')">
        <i class="fas fa-check"></i> Resources
    </button>

    <form action="@Url.Action("Instructions")" method="get" target="_blank">
        <button type="submit">
            <i class="far fa-life-ring"></i> Help
        </button>
    </form>
</div>

Now every other thing works fine but the last button "Help" should redirect to the Instructions action of the Controller but it always redirects to the Index Action of the Controller. I cannot figure out this issue. How can I Redirect to the Instructions action?

My Controller Code for it:-

[Route("Schools/{schoolId:int}/VC/EmergencyRoom")]
public EmergencyRoomController(IEmergencyRoomRepository emergencyRoomRepository,
    EmergencyRoomService emergencyRoomService, ISessionRepository sessionRepository)
{
    this.emergencyRoomRepository = emergencyRoomRepository;
    this.emergencyRoomService = emergencyRoomService;
    this.sessionRepository = sessionRepository;
}

[HttpGet("{sessionId:long}")]
[ResponseCache(NoStore = true)]
public async Task<IActionResult> Index(long sessionId, int schoolId)
{
    Sessions session = await this.sessionRepository.GetSessionAsync(sessionId, 
        this.User.GetUserId());
    if (session == null)
    {
        return NotFound();
    }
   
    ViewData["SchoolId"] = schoolId;
    return this.View(session.Id);
}

[HttpGet("Instructions")]
[AllowAnonymous]
public ViewResult Instructions()
{
    return this.View();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change the Help form like this:

<form action="Instructions" method="get" target="_blank">
    <button type="submit">
        <i class="far fa-life-ring"></i> Help
    </button>
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...