这就是我做的.
问题是没有找到/ signalr/hubs(抛出404).
我的项目有区域,结构如下所示:
signalR的所有脚本都在Scripts文件夹中,我的SignalRTestController.cs看起来像这样:
namespace SignalRTest.Controllers
{
public class SignalRTestController : Controller
{
public ActionResult Index()
{
// Do some work here
// Broadcasting over a Hub from outside of a Hub
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.say("Hello SignalR!");
return View();
}
}
[HubName("MyHub")]
public class MyHub : Hub
{
public void Say(string message)
{
Clients.sendMessage(message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何我的Index.cshtml都引用了所有的javascripts和/ signalr/hubs,如下所示://其他Javascripts
script type …
这是我的情景.
创建了两个具有公共属性名称的模型
public class SimpleModel1
{
// Some Properties
public string Property1 { get; set; }
}
public class SimpleModel2
{
// Some Properties
public string Property1 { get; set; } // Same name as Property1 in SimpleModel1
}
Run Code Online (Sandbox Code Playgroud)在Action(例如Index)中使用SimpleModel1,返回看起来像这样的视图
@model MvcApplication2.Models.SimpleModel1
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("Test", "Home", FormMethod.Post))
{
<label>Enter something here</label>
@Html.TextBoxFor(m => m.Property1)
<button type="submit">Submit</button>
}
Run Code Online (Sandbox Code Playgroud)将值提交给将SimpleModel1作为参数的Test操作,做一些工作,并返回一个接受SimpleModel2的视图
public ActionResult Test(SimpleModel1 model)
{
SimpleModel2 newModel = new SimpleModel2();
// Do Something
newModel.Property1 = "Something different …Run Code Online (Sandbox Code Playgroud)