【解決方法】ボタンのクリック時に2つのパラメーターを使用してコントローラーメソッドにリダイレクトする方法


プレビューボタンがあります。 プレビューボタンをクリックすると、ビューを表示したい。 しかし、ボタンをクリックすると、クエリ文字列として2つのパラメーターを渡しました。 しかし、そのパラメータをURLに表示したくありません。 別の方法は何ですか?
助けてください..

私が試したこと:

関数 GetId (tempid) {
var ProductId = getParameterByName(‘productid’);
var url = “../Template/GetDataForTemplete?TempleteID=” + tempid + “&ProductID=” +ProductId;
window.location.href = url;

}

public ActionResult GetDataForTemplete(int TempleteIDint ProductID)
{
EventViewModel モデル = 新しい EventViewModel();
モデル = GetData(製品 ID);
場合 (TempleteID == 1)
{
return View(“Templete1”, モデル);
}
}

解決策 1

フォームを使用する

@using (Html.BeginForm("GetDataForTemplete", "Home", FormMethod.Post, new { id = "myForm" }))
{
    <input type="hidden" name="templeteID" id="templeteID" />
    <input type="hidden" name="ProductID" id="ProductID" />
    
    <input type="submit" value="Submit" />
}

<script type="text/javascript">
    function GetId(tempid) {
        // set the form variables to whatever you want
        $("#myForm #templeteID").val(tempid);
        $("#myForm #ProductID").val(2);
        // this code is getting called from the form submit so the form is
        // being submitted anyway.  If your code is called from a click event
        // you'll need to submit the form as below
        //$("#myForm").submit();
    }

    // I'm using the submit event of the form to run GetId, you
    // can use a different method if desired
    $(document).ready(function(){
        $("#myForm").submit(function(e){
            // pass your proper tempid
            GetId(1);
        });
    });
</script>

解決策 2

ajaxも使えます。
最初に ajax CDN を追加します

<本体>

jquery id selectorjQuery Selectors を使用して要素からデータを取得することもできます[<a href=”https://www.w3schools.com/jquery/jquery_ref_selectors.asp” target=”_blank” title=”New Window”>^</a>].データパスを取得したら
それをajax呼び出しで

<script type="text/javascript">

関数 redirectUrl(パラメーター 1、パラメーター 2) {
var URL = url + “https://www.codeproject.com/” + パラメータ + ‘?EmpId=’ + パラメータ 2;

$.ajax({
url: URL、
タイプ:「GET」、
成功: 関数 (データ) {
$(‘#partialView’).html(データ);
読み込み中 (‘閉じる’);
$(‘#divLoding’).hide();
$(‘#partialView’).show(500);
}、
error: function (data) { alert(“redirectUrl() でエラーが発生しました!!”); $(‘#divLoding’).hide(); }
});
}

</script>

コメント

タイトルとURLをコピーしました