如何将数据从视图传递到控制器或在aspnet Core MVC中的控制器之间传递数据

时间:2016-12-27 04:07:33

标签: asp.net-core-mvc

您好,我会直接解决我的问题 我有RiddleController有这样的2个IActionResult

    [HttpGet]
    public IActionResult Edit(int id){
        ViewBag.ChapterOpt = _context.Chapters.OrderBy(c => c.ID).ToList();
        string referrer = Request.Headers["Referer"].ToString();
        ViewBag.referrer = referrer;
        var riddle =_context.Riddles.Find(id);
        return View(riddle);
    }

    [HttpPost]
    public IActionResult Edit(Riddle riddle){
        if(ModelState.IsValid)
        {
            // how can i get the referrer from Edit(GET) upthere to 
            // this referrer
            // string referrer = Request.Headers["Referer"].ToString();

            var qt = _context.Riddles.Find(riddle.ID);
            qt.ChapterID  = riddle.ChapterID;                
            qt.Content  = riddle.Content;
            qt.Suggestion  = riddle.Suggestion;
            _context.SaveChanges();
            return RedirectToAction("Index","Riddle");
        }
        else
        {
            return View();
        }
    }

这是我的观点

@using MathTestApp.Models
@model MathTestApp.Models.Riddle
@{
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
var lstChapter = ViewBag.ChapterOpt;
var referrer = ViewBag.referrer;
}
@section heading
{
Edit Riddle
}
@using (Html.BeginForm("Edit","Riddle",FormMethod.Post)) 
{
    <input id="referrer" type="hidden" value="@ViewBag.referrer" />
    <div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <label for="ChapterID" class="col-md-2 control-label">Chapter</label>
        <div class="col-md-6">
            @Html.DropDownListFor(model => model.ChapterID,new SelectList(lstChapter,"ID","Name"),"-- Chapter --",htmlAttributes: new { @class = "form-control width-180", @id = "ChapterID"})
            @Html.ValidationMessageFor(model => model.ChapterID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label for="Content" class="col-md-2 control-label">Content</label>
        <div class="col-md-6">
            <div class="">
                @Html.TextAreaFor(model => model.Content,  new { id="Content", placeholder="Content", @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        <label for="Suggestion" class="col-md-2 control-label">Suggestion</label>
        <div class="col-md-6">
            <div class="">
                @Html.TextAreaFor(model => model.Suggestion,  new { id="Suggestion", placeholder="Suggestion", @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Suggestion, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="text-center">
        <a class="btn btn-primary btn-md" href="@referrer"><i class="fa fa-rotate-left fa-fw"></i> Back</a>
        <button type="submit" id="savebtn" class="btn btn-success">Save</button>
    </div>
</div>
}

你能告诉我如何将引用来自Get Action方法传递给Post Action方法吗?

非常感谢!

P / S:顺便说一下,我不想创建新的ViewModel来解决这个问题,还有别的方法请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试:

1)将ViewBag.referrer添加到视图中的隐藏字段。

<input type="hidden" name="referrer" value="@ViewBag.referrer" />

2)并在post方法中添加一个新参数:

[HttpPost]
public IActionResult Edit(Riddle riddle, string referrer) {
    if(ModelState.IsValid)
    {
        var qt = _context.Riddles.Find(riddle.ID);
        qt.ChapterID  = riddle.ChapterID;                
        qt.Content  = riddle.Content;
        qt.Suggestion  = riddle.Suggestion;
        _context.SaveChanges();
        return RedirectToAction("Index","Riddle");
    }
    else
    {
        return View();
    }
}