在我的應用程序中,我使用Jquery發送包含字符串值的post請求,然後在控制器中傳遞字符串值用於從API獲取一些數據。ASP.NET內的Jquery和Ajax如何使用模型更新div
下一個控制器返回一個視圖和模型與API中的數據。模型中的這些數據我想放在少數文本框的值中。
主要問題是,我不知道如何刷新模型值的div。當我使用後表單沒有jQuery和ajax它的作品,但我想要做到異步。
JS腳本與崗位要求:
<script type="text/javascript">
$(document).ready(function() {
$('#btn').click(function() {
var data1 = $('#textBox').val();
$.ajax({
url: '/Home/Fill',
type: 'POST',
dataType: 'string',
data: { number:data1 },
success: function (data) {
}
});
});
});
控制器:
[HttpPost]
public ActionResult Fill(string number)
{
CompanyInfo Info = new CompanyInfo();
//Here some API actions
return View("Index",Info);
}
股利與索引視圖中的模型值:
<div id="target">
<h1>Number:</h1>
<input type="text" value="@Html.DisplayFor(m => m.Num)" id="Number" />
<br />
<h1>Company name:</h1>
<input type="text" value="@Html.DisplayFor(m=>m.CompanyName)" id="CompanyName" />
<br />
<h1>Street Address</h1>
<input type="text" value="@Html.DisplayFor(m=>m.StreetAddress)" id="Address" />
</div>
非常感謝您的答覆。我使用了這個Json選項,一切正常。我非常感謝你的幫助和分享知識。 –