簡體   English   中英

單擊按鈕時從視圖調用帶有參數的控制器方法(cshtml、c#、MVC)

[英]Calling a Controller Method with parameters from a View when a button is clicked (cshtml, c#, MVC)

這是我的控制器,我想在其中調用方法 AnswerASelect。

namespace VotingWebApp.Controllers
{
   public class HomeController : Controller
   {

    public async Task<IActionResult> Home()
    {
        return View(await _context.QuestionItem.ToListAsync());
    }

    public async Task<IActionResult> Answer(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var questionItem = await _context.QuestionItem.SingleOrDefaultAsync(m => m.ID == id);
        if (questionItem == null)
        {
            return NotFound();
        }

        return View(questionItem);
    }


    public async Task<IActionResult> AnswerASelect(int? id)
    {
        var questionItem = await _context.QuestionItem.SingleOrDefaultAsync(m => m.ID == id);
        questionItem.AnswerAVote = questionItem.AnswerAVote + 1;
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    }

這是我的視圖,在按鈕上單擊我想以模型為參數調用 AnswerASelect 方法,例如... AnswerASelect(model)

@model VotingWebApp.Models.QuestionItem

 @{
  ViewData["Title"] = "Answer Question";
 }

 <h2>@Html.DisplayFor(model => model.Question)</h2>


 <div class="container">
    <button type="button" class="btn btn-info btn-lg" onclick= "CALL AnswerASelect(Model)">@Html.DisplayFor(model => model.AnswerA)</button>

我希望找到一個簡單的解決方案,非常感謝您提前。 我是 Web 應用程序開發的新手,我發現這很難學習。 我看過一些類似的帖子,但我對它們並不是很了解,因此很難將解決方案應用於我當前的問題。

 <h4 class="card-title addbutton">
        <button type="button" onclick="CreateModelDialog('@Url.Action("Create", "Controller",new {restaruntId =Model.RestaurantId})')" class="btn btn-info btn-rounded m-t-10  modal-link">Add</button>
    </h4>

  function CreateModelDialog(url) {
            debugger
            $(".modal-backdrop").remove();
            $('#modal-container').html("");
            $(".modal-backdrop").show();
            $(".stream-loader").show();
            $.get(url, function (data) {

                $(".stream-loader").hide();
                $('#modal-container').html("");
                $('#modal-container').html(data);
                debugger
                $('#modal-container').attr("style", "display:block !important");
            });
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM