如何使用http post渲染局部视图 [英] How do I render a partial view with http post
本文介绍了如何使用http post渲染局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示一些来自公司ID选择的数据库的表格
在ddl上更改我向控制器发送部分视图的jquery帖子,尽管如此,数据正在进入循环html没有渲染。
当我直接在partilal视图中放入一个数字时,我得到表格html。
这是'事件' :
I am tring to show some tables from db selected by company id
on ddl change I a jquery post to the controller that rendering a partial view and allthough, the data is entering the loops the html is not rendered.
when I put a number in the partilal view directly I get the tables html.
this is the 'event':
$("#CompniesList").on('change', function () {
var id = $("#CompniesList option:selected").val();
var jsonData = { companyId: id };
$.post('/CompanyReceapts/GetReceapts/',
jsonData,
function (response) {
if (parseInt(id) > 0) {
$('.tblReceapt').show();
}}
);
});
这是控制器:
this is the controller:
[HttpPost]
public PartialViewResult GetReceapts(string companyId)
{
int id;
int.TryParse(companyId, out id);
var companies = new ReceaptsApp.Models.ReceaptModel.Companies();
// ViewData["CompanyList"] = new SelectList(companies.CompaniesList, "Value", "Text");
// ViewBag.CompanyId = id;
var receaptListCompany = new ReceaptsListForCompany_VM
{
CompanyName = companies.CompaniesList.SingleOrDefault(x => x.Value == id.ToString()).Text,
CompanyId = id,
};
receaptListCompany.Receapts = receaptListCompany.GetReceaptsForCompany(id);
return PartialView("ViewReceapt", receaptListCompany);
}
这是局部视图
and this is the partial view
@using ReceaptsApp.ViewModels
@model ReceaptsApp.ViewModels.ReceaptsListForCompany_VM
<script src="../../Scripts/angular.min.js"></script>
@{
int id = Model.CompanyId;
List<CompanyReceapt> receaptList = Model.Receapts;//this equals to //Model.GetReceaptsForCompany(id)
// List<CompanyReceapt> receaptList = Model.GetReceaptsForCompany(1); //when I try //this I do get the tables
}
@if (receaptList != null) {
foreach (var receapt in Model.Receapts)
{
<table class="tblReceapt" style="display: block">
<tr>
<td>Receapt No.</td>
<td>@receapt.ReceaptId</td>
<td></td>
<td>Date</td>
<td>@receapt.Date</td>
</tr>
<tr>
<td>Job </td>
<td>hours</td>
<td>hourly price</td>
<td>comments</td>
<td>sum</td>
</tr>
@foreach (var item in receapt.Content) {
<tr>
<td>@item.JobEssence</td>
<td>@item.Hours</td>
<td>@item.HuorlyPrice</td>
<td>@item.Comments</td>
<td>@(item.Hours*item.HuorlyPrice)</td>
<td></td>
</tr>
}
</table>
}
}
我尝试了什么:
我尝试使用viewbag而不是模型但得到了相同的结果。
先谢谢大家
What I have tried:
I tryed viewbag instead of model but got the same result.
thank you all in advance
推荐答案
( #CompniesList ).on(' change', function (){
var id =
("#CompniesList").on('change', function () { var id =
( #CompniesList选项:选择)。val();
var jsonData = {companyId:id};
("#CompniesList option:selected").val(); var jsonData = { companyId: id };
.post(' / CompanyReceapts / GetReceapts /',
jsonData,
function (响应){
如果( parseInt (id)> 0 ){
.post('/CompanyReceapts/GetReceapts/', jsonData, function (response) { if (parseInt(id) > 0) {
这篇关于如何使用http post渲染局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文