简体   繁体   中英

Pass data from Html.EditorFor to controller

How do i pass data from a Html.EditorFor to myAction in myController?

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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