所以,首先我已经阅读了大量关于这个特定问题的线索,我仍然不明白如何解决它.基本上,我正在尝试与websocket进行通信,并将收到的消息存储在绑定到listview的可观察集合中.我知道我正在从套接字中正确地获得响应,但是当它尝试将其添加到observable集合时,它会给我以下错误:
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于"发送"以及其他一些事情的信息,但我只是大肆混淆!这是我的代码:
public ObservableCollection<string> messageList { get; set; }
private void MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
string read = "";
try
{
using (DataReader reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
read = reader.ReadString(reader.UnconsumedBufferLength);
}
}
catch (Exception ex) // For debugging
{
WebErrorStatus status = WebSocketError.GetStatus(ex.GetBaseException().HResult);
// Add your specific error-handling code here.
}
if (read != "")
messageList.Add(read); // this is …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AJAX/Jquery和c#写入我的数据库.每当我将参数传递给C#代码时,它都显示为null.我正在使用visual studio在创建控制器类时生成的默认模板.任何帮助,将不胜感激!
NOte:这是我打算打电话给的休息服务.(一个普通的ASP网站......不是MVC.而且,GET Rest api非常有效.)
jQuery的/ AJAX:
var dataJSON = { "name": "test" }
$('#testPostMethod').bind("click", GeneralPost);
function GeneralPost() {
$.ajax({
type: 'POST',
url: '../api/NewRecipe',
data:JSON.stringify(dataJSON),
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
}
Run Code Online (Sandbox Code Playgroud)
C#
//If I remove the [FromBody] Tag then when I click the button this method is never called.
public void Post([FromBody]string name)
{
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我稍微调整了我的代码,但仍遇到同样的问题.回顾一下,它正在加载POST方法,但它传入的是null.
C#
public class RecipeInformation
{
public string name { get; set; }
}
public void Post(RecipeInformation information)
{
}
Run Code Online (Sandbox Code Playgroud)
AJAX:
var dataJSON …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置我的Jquery UI自动完成字段以获取来自ajax连接的数据.到目前为止,这是我的代码:
$("#mainIngredientAutoComplete").autocomplete({
source: function (request, response) {
$.ajax({
url: "../api/IngredientChoices",
dataType: "json",
success: function (data) {
response(function (item) {
return {
label: item.MainName,
value: item.MainItemID
}
});
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的JSON:
[{"SubItemID":1,"MainItemID":1,"SubName":"2%","MainName":"Milk"},{"SubItemID":2,"MainItemID":1,"SubName":"Skim/Fat Free","MainName":"Milk"},{"SubItemID":3,"MainItemID":2,"SubName":"Chedder","MainName":"Cheese"}]
Run Code Online (Sandbox Code Playgroud)
HTML:
<table id="tbl_ingredients" style="padding:0px;">
<tr id="ingHeader">
<td>Ingredient</td>
<td>Measurement</td>
<td>Amount</td>
<td><input id="mainIngredientAutoComplete" /></td>
<td></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
当我开始输入"mil"(用于牛奶)时,我的代码给了我这个错误:

编辑:
我做了一些改变,但有一些尝试,但现在我得到了一个新的错误 -
[URL]第55行第25行未处理的异常
0x800a1391 - Microsoft JScript运行时错误:'data'未定义
$("#mainIngredientAutoComplete").autocomplete({
source: function (request, response) {
$.ajax({
url: "../api/IngredientChoices",
dataType: "json",
response: ($.map(data, function(v,i){
return {
label: v.MainName,
value: v.MainItemID
}})) …Run Code Online (Sandbox Code Playgroud) 我有一个Ajax.BeginForm调用,它应该返回一个局部视图但是将页面重新路由到Action.关于什么是错的任何想法?
这是我要在部分视图上呈现的主页面上的代码:
<div class="col-md-6">
@using (Ajax.BeginForm("Search", "Home", new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "searchResults"
}))
{
<div class="form-group" style="width:85%;">
<div class="right-inner-addon">
<i class=" glyphicon glyphicon-search"></i>
<input type="text" data-autocomplete="@Url.Action("Quicksearch","Home")" class="form-control" placeholder="Search" name="q" />
</div>
</div>
<div class="form-group">
<button class="btn btn-default form-inline" type="submit">Search</button>
</div>
}
<br />
</div>
</div>
<div id="searchResults">
</div>
Run Code Online (Sandbox Code Playgroud)
这是部分视图(由于长度而删除的项目):
<div class="row" id="searchResults">
...removed form elements
<div class="row">
<table class="table">
....stuff
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是控制器:
public PartialViewResult Search(string q)
{
var items = db.items_with_descriptions …Run Code Online (Sandbox Code Playgroud) 我需要一些帮助来实现Dijkstra的算法,并希望有人能够帮助我.我有它,所以它打印一些路线,但它没有捕获路径的正确成本.
这是我的节点结构:
class Node
{
public enum Color {White, Gray, Black};
public string Name { get; set; } //city
public List<NeighborNode> Neighbors { get; set; } //Connected Edges
public Color nodeColor = Color.White;
public int timeDiscover { get; set; }//discover time
public int timeFinish { get; set; } // finish time
public Node()
{
Neighbors = new List<NeighborNode>();
}
public Node(string n, int discover)
{
Neighbors = new List<NeighborNode>();
this.Name = n;
timeDiscover = discover;
}
public Node(string n, NeighborNode …Run Code Online (Sandbox Code Playgroud) 免责声明:我是 ASP.NET Core / Razor / MVC 的新手,正在开始使用 3.0 预览版。
我想要做的是在我的页面上有一个“按钮”,将一个新的空项目添加到列表中,以便用户可以输入一些值。从我所读到的(相当多),听起来像拥有指向控制器的超链接点是正确的方法。我无法让它实际工作。这是我的代码:
指向控制器/动作的链接:
<a class="btn btn-success" asp-controller="Customer" asp-action="AddProduct">New Product</a>
Run Code Online (Sandbox Code Playgroud)
控制器:
public class CustomerController : Controller
{
public void AddProduct()
{
var tmp = "";
}
public string Index()
{
return "This is my default action...";
}
public string Welcome()
{
return "This is the Welcome action method...";
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs 路由是默认的:
app.UseRouting(routes =>
{
routes.MapRazorPages();
});
Run Code Online (Sandbox Code Playgroud)
使用此设置,如果我单击开始按钮,我会看到 URL 更改为以下内容,但没有其他任何反应(例如,未命中断点):
https://localhost:44358/Customers/Create?action=AddProduct&controller=Customer
我试图将路由添加到专门的 UseRouting 代码中,如下所示:
app.UseRouting(routes =>
{
routes.MapRazorPages();
routes.MapControllerRoute(
name: "Customer", …Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.List'.存在显式转换(您是否错过了演员?)
我的代码如下:
public Profile PullActiveProfile()
{
//currentProfile.Decks = new List<string>();
return currentProfile = (from profiles in xmlDoc.Element("PlayerPofiles").Elements("Player")where (string)profiles.Element("Active") == "True"
select new Profile
{
Name = (string)profiles.Element("Name"),
Type = (string)profiles.Element("Type"),
Verified = (string)profiles.Element("Verified"),
Password = (string)profiles.Element("Password"),
Email = (string)profiles.Element("Email"),
Sex = (string)profiles.Element("Sex"),
Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
Created = (DateTime)profiles.Element("Created"),
Birthday = (string)profiles.Element("Birthday") ?? "",
Wins = (string)profiles.Element("Ratio").Element("Win") ?? "0",
Losses = (string)profiles.Element("Ratio").Element("Loss") ?? "0",
Abandoned = (string)profiles.Element("Ratio").Element("Abandoned"),
// The following line is where I get the error. The …Run Code Online (Sandbox Code Playgroud) 我想在单击页面上的按钮时执行我直接在 razor 页面上编写的 C# 方法。我发现,如果我引用了页面上的按钮,它会在第一次加载时执行该方法,但当我实际单击该按钮时,它不会再次执行。这是代码:
Razor 页面 C# 参考:
@functions
{
int productIndex = 0;
int AddProduct()
{
productIndex = productIndex + 1;
return productIndex;
}
}
Run Code Online (Sandbox Code Playgroud)
按钮参考:
<button type="button" class="btn btn-success" onclick="@AddProduct()" value="New Product" />
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个参考并得到相同的结果:
<input class="btn btn-success" onclick="@AddProduct()" value="New Product"/>
Run Code Online (Sandbox Code Playgroud)
我的第二部分问题是如何阻止它在页面加载时执行,以便它只在单击时运行?我找到了参考文献,Page.IsPostBack)但这似乎在客户端上未被识别。
我收到以下错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'Munchkin.Model.PlayerProfiles.Profile'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
Profile currentProfile;
public Profile ActiveProfile()
{
currentProfile = new Profile();
return currentProfile =
(from profiles in xmlDoc.Element("PlayerPofiles").Element("Online").Elements("Player")
where (string)profiles.Element("Active") == "True"
select new Profile
{
Name = (string)profiles.Element("Name"),
Sex = (string)profiles.Element("Sex"),
Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
Created = (DateTime)profiles.Element("Created"),
Birthday = (string)profiles.Element("Birthday"),
Wins = (string)profiles.Element("Ratio").Element("Win"),
Losses = (string)profiles.Element("Ratio").Element("Loss"),
Abandoned = (string)profiles.Element("Ratio").Element("Abandoned")
});
}
Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
参数字典包含非可空类型'System.Int32'的参数'id'的空条目,用于'RecipeTracker.Controllers.StandardDirectionsController'中的方法'System.String Get(Int32)'.可选参数必须是引用类型,可空类型,或者声明为可选参数.
我在我的全局文件中定义了这个:
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
App_Data.databaseDataContext _context = new App_Data.databaseDataContext();
// GET api/<controller>
public List<string> Get()
{
var direction = (from d in _context.ViewStandardDirections("-1")
select d.Direction);
return direction.ToList();
}
// GET api/<controller>/5
public List<Models.DirectionChoices> Get([FromUri]string q)
{
var choices = (from i in _context.ViewStandardDirections(q)
select new Models.DirectionChoices
{
text = i.Direction
});
return …Run Code Online (Sandbox Code Playgroud) c# ×7
ajax ×3
asp.net-core ×2
asp.net-mvc ×2
jquery ×2
linq ×2
razor ×2
algorithm ×1
dijkstra ×1
jquery-ui ×1
json ×1
razor-pages ×1
rest ×1
web-services ×1
xaml ×1