将一个字符串从Controller绑定到View上的文本框

时间:2013-06-19 09:01:42

标签: jquery asp.net-mvc

我想将从Controller操作返回的字符串绑定到View上的文本框。任何帮助将不胜感激。 这是我的控制器代码:

public ActionResult Display(ViewModel.CompanyViewModel entityViewModel)
        {           
            int size=3;

            string password="0123456789";
            Random rnd = new Random();
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(size);

            for (int i = 1; i <= size; i++)
            {
                stringBuilder.Append(password[rnd.Next(password.Length)]);
            }
            Managers.CompanyManager companyManager = new Managers.CompanyManager();
            entityViewModel.SequentialId = stringBuilder.ToString();
            int isCustomerIDExists = companyManager.isExistsSequentialID(entityViewModel);
            if (isCustomerIDExists > 0)
            {
                for (int i = 1; i <= size; i++)
                {
                    stringBuilder.Append(password[rnd.Next(password.Length)]);
                }            
            }
            string str = stringBuilder.ToString();

            return Json(new { str = str }, JsonRequestBehavior.AllowGet);
        }

这是我的View-Javascript代码:

<script type="text/javascript">

    function btnNextAvailable_OnClick() {
        $("#nextAvailableButtonClick.val('true')");
        var mode = $.hash.getValue("m");
        var urlInsert = '@Url.Action("Display")';
        $.getJSON(urlInsert, function () {
        });
        document.getElementById("SequentialId").value = '@Url.Action("Display")';
    }
</script>

我想将从Controller操作返回的字符串str绑定到视图上的文本框“SequentialID”。

这是我在视图上的代码:

function btnNextAvailable_OnClick() {
        $("#nextAvailableButtonClick.val('true')");
        var mode = $.hash.getValue("m");
        var urlInsert = '@Url.Action("Display")';
        $.get(urlInsert, function () {
        })
       $.ajax({
            type: "POST",
            url: '/CompanyController/Display',
            async: false,
            data: {},
            dataType: "json",
            success: (function (data) {
                $('#SequentialId').value(data);
            }),
             error: (function () {
                alert("error");
            })
        });
    }

2 个答案:

答案 0 :(得分:1)

您可以在ViewBag中存储字符串,然后附加到文本框

控制器:

ViewBag.MyString = "sample text";

查看:

<input type="text" value="@ViewBag.MyString" />

对于Json结果

$.get('/Controller/Action', function(data){
   document.getElementById("SequentialId").value = data.str;
});

答案 1 :(得分:1)

试试这个,

function btnNextAvailable_OnClick() {
    $("#nextAvailableButtonClick.val('true')");
    var mode = $.hash.getValue("m");
    var urlInsert = '@Url.Action("Display")';        
   $.ajax({
        type: "GET",
        url: urlInsert,
        async: false,
        data: {},
        dataType: "json",
        success: (function (data) {
        var response = JSON.parse(data);
            $('#SequentialId').val(response.str);
        }),
         error: (function () {
            alert("error");
        })
    });
}