简体   繁体   English

在执行表单操作后将参数传递回视图吗?

[英]Passing a parameter back to a view after performing a form action?

I have a view that loads a record with a certain record number. 我有一个视图,它加载具有特定记录号的记录。 Once the page is loaded, it gives the user an opportunity to login for additional information. 页面加载后,将为用户提供登录其他信息的机会。 Once the login logic is performed, I need to return to that same view with the same record number intact. 一旦执行了登录逻辑,我需要返回具有相同记录编号的相同视图。 I am passing the record number to the action using a hidden input in the form. 我正在使用表单中的隐藏输入将记录号传递给操作。 What I can't seem to figure out is how to return to that same view and provide it with that record #. 我似乎无法弄清楚的是如何返回相同的视图并为其提供记录#。 The code I am trying below is not working. 我在下面尝试的代码无法正常工作。 I know this is MVC 101 stuff but a hint in the right direction would be appreciated, or feel free to scrap my method and suggest something better! 我知道这是MVC 101的东西,但是朝正确方向的提示将不胜感激,或者随时取消我的方法并提出更好的建议!

Form in view: 视图形式:

 <form action="/MyView/Authenticate/@item.ID" method="post" enctype="multipart/form-data">
      <input name="form_id" type="hidden" value="@item.ID">
      .....

Form action: 表单动作:

    [HttpPost]
    public ActionResult Authenticate()
    {
        int myid = Convert.ToInt16(Request["form_id"]);
        .....
        return View("Index",  new { id = myid } );
    }

EDIT: 编辑:

It turns out that the correct view is being returned, but it is expecting a model item type of "JobSummaryModel" per the Index action result below. 事实证明,将返回正确的视图,但是根据下面的Index操作结果,期望模型项类型为“ JobSummaryModel”。 So the question I actually need answered is, how do I pass both the record id and this view model to it? 因此,我实际上需要回答的问题是,如何将记录ID和此视图模型传递给它?

    public ActionResult Index(int id = 0)
    {
        List<JobSummaryModel> jdata;

        ViewBag.IsResults = false;

        if (id != 0)
        {
            ViewBag.IsResults = true;            
        }
        jdata = db.Jobs.Where(c => c.ID == id).Select(c => new JobSummaryModel() { ID = c.ID, Name = c.Name, City = c.City, PostalCode = c.PostalCode, JobDescription = c.PositionDescription }).ToList();
        return View(jdata);
    }

EDIT: 编辑:

Thanks Reddy, your suggestions worked! 感谢Reddy,您的建议有效! My only remaining issue is that when I return to my Index view from the Authenticate action, I do not seem to have my "jdata". 我唯一剩下的问题是,当我从Authenticate操作返回到索引视图时,我似乎没有“ jdata”。 Is my Index action result not being rerun when I return the Index view via my Authenticate action? 通过身份验证操作返回索引视图时,是否不会重新运行我的索引操作结果? I am coming from a web forms background where, in an instance like this, the Load/Init events would automatically run when a form is loaded. 我来自Web表单背景,在这样的实例中,加载表单时,Load / Init事件将自动运行。 Do I need to bind my "jdata" in the Authenticate action and include it in the viewmodel? 我是否需要在Authenticate操作中绑定“ jdata”并将其包含在viewmodel中?

EDIT: Resolved. 编辑:已解决。 Changed my "return View" to a "return RedirectToAction" to resolve my final issue. 将我的“返回视图”更改为“返回RedirectToAction”以解决我的最后一个问题。 Thanks everyone! 感谢大家!

You are better off creating a ViewModel for this like so: 您最好像这样创建一个ViewModel:

Create a View Model class ie 创建一个视图模型类,即

public class AuthViewModel
{
    public int MyId { get; set; }
}

In your View put the following directive at the top: 在您的视图中,将以下指令放在顶部:

@model AuthViewModel

In your initial [HttpGet] method return the view model: 在您的初始[HttpGet]方法中,返回视图模型:

[HttpGet]
public ActionResult Authenticate()
{
    var model = new AuthViewModel { MyId = 123 };
    return View("Index", model );
}

It's best to use Html helpers in your view, so you can change it to this: 最好在视图中使用HTML帮助器,因此可以将其更改为:

@using(Html.BeginForm()
{
    @Html.HiddenFor(m => m.MyId)
    ...
}

The above uses naming conventions to post back to the action that you are on. 上面使用命名约定将其回发到您正在进行的操作。

Then return it to your view like this: 然后将其返回到您的视图,如下所示:

[HttpPost]
public ActionResult Authenticate(AuthViewModel model)
{
    int myid = model.MyId;
    return View("Index", model );
}

Then you can output using this razor syntax @Model.MyId 然后,您可以使用此剃刀语法@Model.MyId输出

It's really worth doing some tutorials to learn the conventions, a small amount of time invested in this will save you a lot of time in the future. 确实值得做一些教程来学习这些约定,为此花费少量时间将为您节省很多时间。

Answer For your after Edit: 对于您的修改后答案:

All you want to pass to view is a int Id and your List<JobSummaryModel> jdata right? 您只想传递一个int Id ,您的List<JobSummaryModel> jdata对吗?

So create a ViewModel JObSummaryModelHelper 因此,创建一个ViewModel JObSummaryModelHelper

Public class JObSummaryModelHelper
{
   public int Id {get;set;}
   public List<JobSummaryModel> jdata {get;set;}
}

Now in your controller 现在在您的控制器中

public ActionResult Index(int id = 0)
    {
        JObSummaryModelHelper jobDetails = new JObSummaryModelHelper();
        jobDetails.Id = id;           

        ViewBag.IsResults = false;

        if (id != 0)
        {
            ViewBag.IsResults = true;            
        }
        jobDetails .jdata = db.Jobs.Where(c => c.ID == id).Select(c => new JobSummaryModel() { ID = c.ID, Name = c.Name, City = c.City, PostalCode = c.PostalCode, JobDescription = c.PositionDescription }).ToList();
        return View(jobDetails );
    }

Now make sure your view is set to expect this new viewmodel 现在确保您的视图设置为期望使用新的视图模型

@model JObSummaryModelHelper 

carry on with your manipulation...... 继续进行操作...

Instead of 代替

return View("Index",  new { id = myid } );

could you do 你能做

return Index(myid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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