Asp.net mvc 更改 DIV 内容无需重新加载且无需按钮
Asp.net mvc change DIV content without reload and without button
我是 Asp.net mvc 的新手。我正在尝试编写两个简单的 tcp 应用程序。第一个应用程序是客户端(Windows 形式),第二个应用程序是主机(Asp.net mvc)。在第一个应用程序中,我们可以编写消息并将其发送到第二个应用程序。
我在第二个应用程序中收到了正确的消息,我在 HomeController 中设置了 属性 消息。
我想在消息 属性 中的字符串上更改 div 'messageDiv' 每次我从第一个应用程序发送它并设置 属性 消息时。
下面的代码显示了如何使用按钮制作它。每次我们从第一个应用消息发送,然后在第二个点击接收按钮时,它都会正确更改。
可以在消息 属性?
上写入会更改 div 的间隔
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Waiting for messages : </h2>
@using (Ajax.BeginForm(new AjaxOptions()
{
UpdateTargetId = "messageDiv",
HttpMethod = "Post"
}))
{
<input id="btn" type="submit" value="receive"/>
}
<div id="messageDiv"></div>
HomeController.cs
public class HomeController : Controller
{
public static string message { get; set; }
// GET: Home
[HttpGet]
public ActionResult Index()
{
tcpHost host = new tcpHost();
return View();
}
[HttpPost]
public ActionResult Index(string x)
{
return Content(string.Format(message));
}
}
如果我没理解错的话,您需要什么东西可以在 you/others 收到消息时自动更新您的消息 div?
你需要实现一个real-time communication (recommended) or you could use a setInterval method in JavaScript,它会不断检查是否有一些新消息,如果有,那么它会获取并显示在消息中div(不推荐)。
我是 Asp.net mvc 的新手。我正在尝试编写两个简单的 tcp 应用程序。第一个应用程序是客户端(Windows 形式),第二个应用程序是主机(Asp.net mvc)。在第一个应用程序中,我们可以编写消息并将其发送到第二个应用程序。 我在第二个应用程序中收到了正确的消息,我在 HomeController 中设置了 属性 消息。 我想在消息 属性 中的字符串上更改 div 'messageDiv' 每次我从第一个应用程序发送它并设置 属性 消息时。
下面的代码显示了如何使用按钮制作它。每次我们从第一个应用消息发送,然后在第二个点击接收按钮时,它都会正确更改。
可以在消息 属性?
上写入会更改 div 的间隔Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Waiting for messages : </h2>
@using (Ajax.BeginForm(new AjaxOptions()
{
UpdateTargetId = "messageDiv",
HttpMethod = "Post"
}))
{
<input id="btn" type="submit" value="receive"/>
}
<div id="messageDiv"></div>
HomeController.cs
public class HomeController : Controller
{
public static string message { get; set; }
// GET: Home
[HttpGet]
public ActionResult Index()
{
tcpHost host = new tcpHost();
return View();
}
[HttpPost]
public ActionResult Index(string x)
{
return Content(string.Format(message));
}
}
如果我没理解错的话,您需要什么东西可以在 you/others 收到消息时自动更新您的消息 div?
你需要实现一个real-time communication (recommended) or you could use a setInterval method in JavaScript,它会不断检查是否有一些新消息,如果有,那么它会获取并显示在消息中div(不推荐)。