简体   繁体   English

使用附加字符串将模型从视图传递到控制器

[英]Passing model from view to controller with additional string

I have to pass model to my Action in Controller and i need to pass string too. 我必须将模型传递给Controller中的Action,我也需要传递字符串。

Something like that: 像这样:

<a class="btn" title="Send" href="@Url.Action("Check","Approve",Model && new{redirectTo = "ShowPDF")">Send</a>

Is any way to do that?(For me better way) Or must I add value to Model.Redirect "OnClick" and how to do that? 有什么办法?(对我来说更好的方法)还是我必须为Model.Redirect“ OnClick”增加价值,以及如何做到这一点? (Not wanted way) (不想要的方式)

My solution: 我的解决方案:

<a class="btn" title="Send" href="@Url.Action("Check","Approve",new { ID =Model.ID,EAN =Model.ID_EAN, ic = Model.invoiceNumber, s = Model.supplier, a = Model.amount, d = Model.dueDate, redirect = "ShowPDF"  })">Send</a>

I did it this way, but for me this way is very complicated on model change. 我是这样做的,但是对我来说,这种方式在模型更改时非常复杂。

Check Action: 检查动作:

public ActionResult Check(string ic, string s, decimal a, DateTime d, int ID, string EAN, string redirect = "Index")
        {
            ...
            return RedirectToAction(redirect);

        }

If you want to send only few properties of your Model, it is fine to follow your solution. 如果您只想发送模型的少量属性,则可以遵循您的解决方案。 But if you want to send a whole lot of properties of a model, keep in mind that there is a limit for how much data querystrings are allowed to carry ( varied by browsers). 但是,如果要发送模型的全部属性,请记住,允许携带多少数据查询字符串(随浏览器而异)是有限制的。

Instead you should send the data in in the form. 相反,您应该以表格形式发送数据。 If you want to send some data via a form, it should be a form element. 如果要通过表单发送一些数据,则它应该是一个表单元素。 You can create an input elemenet or a hidden input element 您可以创建输入要素或隐藏的输入元素

@using(Html.BeginForm("check","approve"))
{
  @Html.HiddenFor(p=>p.ID_EAN)
  @Html.HiddenFor(p=>p.ID)
  <!-- Add a hidden element for other properties you want to send -->
  <input type="hidden" name="redirect" value="ShowPDF" />
  <inpu type="submit" />
}

And you can use the same view model as the param of your HttpPost action method 您可以使用与HttpPost操作方法的参数相同的视图模型

[HttpPost]
public ActionResult Check(YourModel model,string redirect)
{
  // to do : Return something
}

But...... , 但是......

if you are not changing any values in the view, You do not need to send the entire form. 如果您未更改视图中的任何值,则无需发送整个表单。 Simply send the unique Id value of the entity(primary key) and in the HttpPost action method, using that unique Id, get the full entity (query the database) and use that. 只需发送实体(主键)的唯一ID值,然后在HttpPost操作方法中使用该唯一ID,获取完整的实体(查询数据库)并使用它。 Remember, you should never trust the value coming from the client(browser). 请记住,您永远不应信任来自客户端(浏览器)的值。 For this, you can use your solution, you do not need form as you are sending only 2 values. 为此,您可以使用您的解决方案,因为您只发送2个值,所以不需要表格。

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

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