0

I need to get values to two parameters in Button OnClick method.

<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val(),?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val()'+'?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val() & ?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val():?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val() && ?Cost='+ $('#txtCost').val()" />    
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val(),?Cost='+ $('#txtCost').val()" />

I need to get txtRemarks & txtCost textbox values into two parameters on Button OnClick method.

1

2 Answers 2

0

You can implement your requirement by jQuery like this

Update cshtml

<input type="text" id="txtRemarks" value="Test" />
<input type="text" id="txtCost" value="10" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />

Add script tag to cshtml

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function() {
       updateAttr();
    });
    $("#txtRemarks, #txtCost").change(function() {
        updateAttr();
    });

    function updateAttr() {
        $('input[type="button"]').each(function(index, item) {

            var onclick = 'location.href=';
            onclick += '@Url.Action("ServiceDetails", "ServiceHeaders")';

            onclick += "?Remark=" + $('#txtRemarks').val();
            onclick += "?Cost=" + $('#txtCost').val();
            $(item).attr('onclick', onclick);
        });
    }

</script>

I tested and it worked

Sign up to request clarification or add additional context in comments.

Comments

0

I would rely on the standard ASP.NET MVC helpers:

chtml

@using (Html.BeginForm("AddServiceHeaders", "ServiceDetails", FormMethod.Post))  
{ 
    @Html.EditorFor(model => model.Remark)  
    @Html.EditorFor(model => model.Cost)  

    <input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
}

c#

public sealed class Model {
    public string Remark { get; set; }
    public int Cost { get; set; }
}

public class ServiceDetailsController : Controller {

    [HttpPost]
    public ActionResult AddServiceHeaders(Model model){
        // ..
    }

}

See example: Music Store.

I would recommend to:

  • use POST-verb for any data update (pass params to the server in the request body, not the query string)
  • use input-validation
  • protect your data from malicious actions - see AntiForgeryToken

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.