ActionLink to submit Model value

巧了我就是萌 提交于 2019-12-13 16:37:45

问题


I want my Ajax.ActionLink to pass a viewModel property to action.

Here is my ViewModel

public class ViewModel
    {
        public string Searchtext { get; set; }
    }

My .cshtml

@Ajax.ActionLink("Bottom3", "Bottom3",new { name = Model.Searchtext}, new AjaxOptions
{
    HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "pointsDiv"
})

 using(Html.BeginForm("Bottom3", "Home", FormMethod.Get))
     {
         @Html.TextBoxFor(x => x.Searchtext)
         <button type="submit">Search</button>
     }
    <div id="pointsDiv"></div>
}

My Controller action:

public PartialViewResult Bottom3(string name)
        {
            var model = db.XLBDataPoints.OrderBy(x => x.DataPointID).Take(3).ToList();

            return PartialView("Partial1", model);
        }

But the name parameter passed to the action is always null. How do I solve this?


回答1:


In your code... you have 2 different ways of posting to the server: the link and the form button.

The problem is that the ActionLink has no way to get the value from the input in client side... just the original value.

If you press the Search button, you will see a value posted.

Now, you can use some jQuery to modify a standard ActionLink (not the Ajax.ActionLink): https://stackoverflow.com/a/1148468/7720

Or... you can transform your Form in order to do a Ajax post instead of a normal one: https://stackoverflow.com/a/9051612/7720




回答2:


I did this for a model of mine like so. I ONLY supported the HttpPost method. So add the HttpMethod="POST" to your Ajax.ActionLink

    [HttpPost]
    public ActionResult Accounts(ParametricAccountsModel model)
    {
        if (model.Accounts == null)
        {
            GetAccountsForModel(model);
        }
        if (model.AccountIds == null)
        {
            model.AccountIds = new List<int>();
        }
        return View(model);
    }

On the razor view

@Ajax.ActionLink(
        "Add Account to Order", "Accounts", "Parametric", null,
        new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "...", HttpMethod = "POST" },
        new { @id = "AddParametricAccountLink" })  

The model has a list of selected account ids. So in javascript, I modified the href of the action link dynamically.

function UpdateParametricAccountAction() {
    var originalLink = '/TradeNCashMgmt/Parametric/Accounts';
    var append = '';
    var numberOfRows = $('#ParametricAccounts').find('.parametric-account-  row').size();
    for (var i = 0; i < numberOfRows; i++) {
        if (i != 0) {
            append += '&';
        }
        else {
            append = '?';
        }
        var idValue = $('#NotionalTransactionsAccountId_' + i).val();
        append += 'AccountIds%5B' + i + '%5D=' + idValue;
    }

    $('#AddParametricAccountLink').attr('href', originalLink + append);
}

Since the model binder looks for parameter names in the query string and form submission, it will pick up values using the href. So I posted a model object using the querystring on my Ajax.ActionLink. Not the cleanest method, but it works.



来源:https://stackoverflow.com/questions/24353492/actionlink-to-submit-model-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!