我想将JSON的结果存储在属性中,但我不知道如何.我正在使用ASP.NET和MVC 4/Razor.
我的模型的构造函数是:
public UserModel()
{
WebClient request = new WebClient();
string response = request.DownloadString(url);
JObject _naturalUserObject = (JObject)JsonConvert.DeserializeObject(response);
}
Run Code Online (Sandbox Code Playgroud)
我有以下属性:
[Key]
public int Id { get; set; }
public int DisplayName { get; set; }
public int Avatar { get; set; }
Run Code Online (Sandbox Code Playgroud)
要从我的JSON访问一个简单的值:
_naturalUserObject["users"][0]["name"];
Run Code Online (Sandbox Code Playgroud)
最后一个问题是:如何将我的JObject中的每个值存储在模型的属性中,最后将其显示在我的视图中?
提前致谢.
/编辑/ 我的模特现在是:
public UserModel()
{
WebClient request = new WebClient();
string response = request.DownloadString(String.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}", ConfigurationManager.AppSettings["SteamApiKey"].ToString(), HttpContext.Current.Request.Cookies["SteamIdCookie"].Value));
string _naturalUserObject = JsonConvert.DeserializeObject<string>(response);
}
Run Code Online (Sandbox Code Playgroud)
我的一个属性是:
private string _avatar;
public string Avatar
{
get { return _avatar; …Run Code Online (Sandbox Code Playgroud) 使用 KnockoutJS 从 observableArray 中删除一个项目。
\n\n我不知道 \xe2\x80\x94 没有发生任何事情。
\n\n我的标记:
\n\n<button data-bind="click: Summary.remove">\n <i class="icon-ok"></i>\n</button>\nRun Code Online (Sandbox Code Playgroud)\n\n我的删除操作:
\n\nself.remove = function (item) {\n self.products.remove(item);\n};\nRun Code Online (Sandbox Code Playgroud)\n\n我的可观察数组:
\n\nself.products = ko.observableArray();\nRun Code Online (Sandbox Code Playgroud)\n\n没有什么。这self.remove功能正在触发,但该项目没有任何反应。
我认为我传递的细节已经足够了,但如果您需要更多信息,请告诉我。
\n在a中声明一些变量switch并在外部使用它.
我不知道语法.
为了解决我的问题,我想做这样的事情,但是不行,因为它只是一种哲学:
public ActionResult Compare(int id, string orderBy)
{
var productsList = Products.BuildIndividualProductComparisonList(id);
var product = Products.BuildToCompare(id);
switch (orderBy)
{
case "lowestToBiggest":
var organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
case "biggestToLowest":
var organizedProductsList =
productsList.OrderBy(x => x.maxProductPrice);
break;
default:
var organizedProductsList =
productsList.OrderBy(x => x.minProductPrice);
break;
}
ComparisonViewModel comparisonViewModel =
new ComparisonViewModel
{
Product = product,
ProductList = organizedProductsList
};
return View(comparisonViewModel);
}
Run Code Online (Sandbox Code Playgroud)
这是我最初的C#代码,效果很好:
public ActionResult Compare(int id, string orderBy)
{
var productsList …Run Code Online (Sandbox Code Playgroud) 将List中所有元素的每个id加入到类似的连接字符串中1, 2, 3.
在我的应用程序中有一个方法,其唯一参数是string类型,我想传递一个类似的值1, 2, 3, 4, 5.
那些值(1, 2, 3, 4, 5)将通过a发送给我List<Product>- 我需要id从这个列表中获取每个元素.
如何获取这些ids并将它们连接成一个字符串?
我已经尝试过以下代码(基于这个问题):
List<Products> sessionProducts = sessionStore.Get("ProductsSummary");
// ProductsSummary is the index of my Session.
string list = string.Join(", ", sessionProducts);
return Content(list);
Run Code Online (Sandbox Code Playgroud)
但没有成功.当我访问我的视图时,返回的是:
MyApp.Models.Data.Products
我的.Net版本是4.5.
我认为这不是因为我在Google和SO上搜索,但没有成功.而且,我发现的并没有帮助我.
获取并突出显示a中小数点之间的最低价格foreach.
我正在比较一些市场之间的各种产品价格.结果显示在HTML中table.该表的最后一行包含总价值,即每个产品每个市场价格的总和.
例如:
Market 1 Market 2
Xbox 360 US$119,00 US$125,00
Playstation 3 US$129,00 US$119,00
Total US$248,00 US$244,00
Run Code Online (Sandbox Code Playgroud)
View背后的代码是:
@foreach (var item in Model.Collection.Products.DistinctBy(p => p.id))
{
<tr>
<td>
<p>@item.name</p>
</td>
@foreach (var market in Model.Markets)
{
<td>
@foreach (var product in Model.Collection.Products.Where
(p => p.productId == item.productId &&
p.marketId == market.id))
{
<p>@product.price</p>
}
</td>
}
</tr>
}
<tr>
@foreach (var market in @Model.Markets)
{
<td class="total">
@{
decimal totalValue =
Model.Collection.GetTotalPrice(market.marketId);
} …Run Code Online (Sandbox Code Playgroud) 显示加载的地图.map_canvas而不是#map_canvas.
未捕获的TypeError:无法设置undefined的属性"position"
当我在#map_canvas所有内容中加载地图时,如果我将元素包装器更改为.map_canvas,我的应用程序将会崩溃.
我做了两个例子:
#map_canvas:上播放的jsfiddle ✓.map_canvas:在jsFiddle上玩 ✗在这两种情况下,请注意JavaScript代码的第36行.
提示:我已经尝试过使用jQuery选择器代替
getElementsByClassName(),但问题仍然存在.
有人可以给我一个想法吗?
这是我的代码:
const x = { a: 1, b: undefined }
// x.b is being considered `any` instead of `undefined`
Run Code Online (Sandbox Code Playgroud)
这是演示该问题的屏幕截图:
在操场上,它工作正常,这让我认为这与我的配置有关。但可能是什么原因造成的呢?
就其价值而言,这是我的tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"baseUrl": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"importHelpers": true,
"target": "ESNext",
"module": "esnext",
"lib": ["ESNext", "dom", "DOM.Iterable"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
},
"exclude": ["node_modules", "tmp"]
}
Run Code Online (Sandbox Code Playgroud)
一些额外的信息:
发送true或false关闭窗口时。
当我单击一个按钮时,将打开一个带有window.open();语法的窗口。我需要的似乎很简单:关闭窗口后,返回到打开弹出窗口的窗口,来自服务器的响应可以是true或false-就像Facebook的API一样。
有人知道我该如何简单地做到这一点?
我不想使用jQuery,因为页面的CSS会覆盖弹出窗口的CSS。
HTML:
[...]
<a href="#" class="share" data-networkName="<?php echo $network->name; ?>">
Share</a>
[...]
Run Code Online (Sandbox Code Playgroud)
JS:
$(".share").on("click", function(event) {
event.preventDefault();
var networkName = $(this).data("networkName");
window.open("share.php?network=" + networkName");
});
Run Code Online (Sandbox Code Playgroud) 我的控制器正在检索以下内容:
def index
@segments = Segment.all
@products = Product.all
end
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的.我想基于它来迭代产品segment_id.像这样的东西:
def index
@segments = Segment.all
@products = Product.where(segment_id: x)
end
Run Code Online (Sandbox Code Playgroud)
问题是:如何x从视图传递到控制器?
我的观点是:
- @segments.each do |s|
- @products.each do |p|
This is a product from #{s.name}.
Run Code Online (Sandbox Code Playgroud)
好的,问题出在哪里?我没有展示特定细分的具体产品.我正在展示所有细分的产品.我需要的是这样的:
- @segments.each do |s|
- @products(segment_id: s.id).each do |p|
This is a product from #{s.name}.
Run Code Online (Sandbox Code Playgroud)
你能明白吗?
我有以下代码片段:
HTML
<div class="colors">
<h1>Colors:</h1>
<div class="recipe"></div>
</div>
<div class="numbers">
<h1>Numbers:</h1>
<div class="recipe"></div>
</div>
<div class="people">
<h1>People:</h1>
<div class="recipe"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
var colors = 'yellow / black / purple',
numbers = '5 / 15 / 25',
people = 'Brad Pitt / Leonardo DiCaprio / Anne Hathaway';
$('.colors .recipe').html(colors.replace(/(\w+)\/*/g, '<em>$1</em><br>'));
$('.numbers .recipe').html(numbers.replace(/(\w+)\/*/g, '<em>$1</em><br>'));
$('.people .recipe').html(people.replace(/(\w+)\/*/g, '<em>$1</em><br>'));
Run Code Online (Sandbox Code Playgroud)
我对正则表达式不好,然后我在各自的食谱上渲染分离值时会得到意想不到的结果(你可以在上面发布的jsFiddle上看到更多).
我的意思是,以下结果显示给我:
[...]
布拉德
皮特
/莱昂纳多
迪卡普里奥
[...]
而我想要和需要的是:
布拉德·皮特
莱昂纳多·迪卡普里奥
没有斜线,没有分隔的名字/姓氏.
c# ×4
javascript ×4
html ×2
jquery ×2
asp.net-mvc ×1
json ×1
knockout.js ×1
regex ×1
typescript ×1