【问题标题】:Passing form values from one controller view to another form on a different controllers view in asp MVC在asp MVC中将表单值从一个控制器视图传递到另一个控制器视图上的另一个表单
【发布时间】:2016-05-15 21:50:46
【问题描述】:

好的,我是 MVC 的新手,并试图让我成为一个网页,我可以在其中转移到带有邮政编码和按钮的小表单框,以引用页面,该页面将填写报价页面上的邮政编码部分。

我的问题是我有两个控制器,一个 homeController 有一个带有小表单框的索引视图。我需要将邮政编码传递给 QuoteController,它有自己的视图,其中填充了新的邮政编码。

主控制器输入,indexview

 @using (Html.BeginForm("Quote", "Quote"))    
  <p>Move From Zip:</p>  
 <input type="text" name="Zip"/><br /> 
 <input type="submit" value="Next" name="next">

用于接收 zip 的报价单,在报价控制器上,在报价视图上

@Html.Label("Move From Zip ")<br />
@Html.TextBoxFor(m => m.MoveFromZip, "", new { maxlength = 5, @class =    "short-textbox" })

最简单的方法是什么

【问题讨论】:

  • 这里是家庭控制器输入
  • @using (Html.BeginForm("Quote", "Quote")) {

    从 Zip 移动:


    }

标签: asp.net-mvc session model-view-controller tempdata


【解决方案1】:

在 HomeController 的 Index 视图中,您可以将表单操作保持为 "Quote/Quote"

@using (Html.BeginForm("Quote", "Quote"))
{
  <input type="text" name="Zip" />
  <input type="submit" />
}

QuoteController 中为您的 Quote 操作方法视图创建一个视图模型

public class QuoteVm
{
  public string Zip { set;get;
}

在您的 QuoteController 的 Quote 操作方法中

[HttpPost]
public ActionResult Quote(QuoteVm model)
{
  return View(model);
}

您的报价视图将是

@model QuoteVm
<p>Data passed(POSTED) from Index view</p>
@using(Html.BeginForm("QuoteSave","Quote"))
{  
  @Html.TextBoxFor(s=>s.Zip)
  <input type="submit" />
}

现在对于在这个视图中提交的表单,你需要有另一个 HttpPost 操作方法

[HttpPost]
public ActionResult QuoteSave(QuoteVm model)
{
   // to do : Do something and return something
}

【讨论】:

  • 谢谢它的工作,但我不知道为什么?我只使用了你在我的验证所在的当前 QuoteModel 类中提供的第二个代码块,然后我添加的只是开始表单方法中的项目,其他东西对传输没有影响。
  • 还有另一个问题,当数据传输到报价页面时,它会触发我的所有验证,我该如何防止这种情况发生。
  • @NickBooth 我不知道你的编辑是否会通过,但将来你应该指出你正在修复一个错字(第二个代码块中缺少的大括号),最好是一个链接备份。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多