简体   繁体   English

将数据从Html.EditorFor传递到控制器

[英]Pass data from Html.EditorFor to controller

How do i pass data from a Html.EditorFor to myAction in myController? 如何将Html.EditorFor中的数据传递给myController中的myAction?

This is my Editor: 这是我的编辑器:

<div class="editor-label">
            @Html.LabelFor(model => model.Quote_Currency)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Quote_Currency, new { })
            @Html.ValidationMessageFor(model => model.Quote_Currency)
        </div>

This is my controller action: 这是我的控制器动作:

public ActionResult SaveQuote(FxQuote quote, string Quote_Currency)
        {


                objQuote.Quote_Currency = Quote_Currency;

                dcfx.FxQuotes.InsertOnSubmit(quote);
                dcfx.SubmitChanges();


            return View();

Here,i was trying to have a parameter with the same name as my Editor but that did not work. 在这里,我正在尝试使用一个与我的编辑器同名的参数,但这不起作用。 Please help. 请帮忙。

You can add a FormCollection collection as an extra parameter to your controller method. 您可以将FormCollection collection作为附加参数添加到控制器方法。 That collection will hold all data passed from the view in the controller. 该集合将保存从控制器中的视图传递的所有数据。 You can use the debugger to explore which data is exactly being sent to the controller. 您可以使用调试器来探查哪些数据正正确发送到控制器。

Why dont you just change the action to accept your model? 您为什么不只是更改操作以接受模型?

Something like this: 像这样:

 public ActionResult SaveQuote(ModelType model) 
 { 


            objQuote.Quote_Currency = model.Quote_Currency; 

            dcfx.FxQuotes.InsertOnSubmit(model.Quote); 
            dcfx.SubmitChanges(); 


        return View();
  }

Alternatively you can change it to accept a form collection as mentioned in another answer: 或者,您可以更改它以接受另一个答案中提到的表单集合:

 public ActionResult SaveQuote(FormCollection collection) 

Hope this helps 希望这可以帮助

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

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