考虑一个简单的外汇计算器应用程序的示例。
我可以使用以下方法定义我的模型traitlets:
from traitlets import HasTraits, Float, observe, Enum
import math
class FXModel(HasTraits):
domestic_qty = Float()
foreign_qty = Float()
fx_rate = Float(float('nan')) # in units of domestic_qty/foreign_qty
lock = Enum(['domestic', 'foreign'], default_value='domestic')
_calculating = Enum([None, 'domestic', 'foreign'], default_value=None)
def calc_foreign(self):
if not math.isnan(self.fx_rate):
self._calculating = 'foreign'
self.foreign_qty = self.domestic_qty / self.fx_rate
self._calculating = None
def calc_domestic(self):
if not math.isnan(self.fx_rate):
self._calculating = 'domestic'
self.domestic_qty = self.foreign_qty * self.fx_rate
self._calculating = None
@observe('domestic_qty')
def on_domestic(self, change):
if self._calculating is None: …Run Code Online (Sandbox Code Playgroud) 我一直在尝试让 MVC 应用程序生成 PDF(填充有数据)并提示将其下载给用户。我设置了一个测试方法只是为了看看它是如何完成的,并且我正在尝试在内存中创建文档,因为我知道浏览器并不总是知道如果您只是向其传递字节流该怎么办。
这是我正在使用的方法:
//Test Report
public ActionResult Report()
{
MemoryStream stream = new MemoryStream();
PdfWriter wri = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(wri);
Document doc = new Document(pdf);
doc.Add(new Paragraph("Hello World!"));
doc.Close();
return new FileStreamResult(stream, "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)
每次我尝试加载 Report() 方法时,都会收到一条错误消息,指出该流已关闭,因此无法访问。我研究了几种不同的解释来解释为什么会出现这种情况,但所有这些似乎都适用于 iTextSharp 和 iText 5,因此这些解决方案不起作用。
我在这里做错了什么?
我想从远程计算机或移动设备(调用)调试我的 ASP.NET Core 应用程序?请帮我。在标准 .net 中,在.vs\config\applicationhost.config更改其工作状态时<binding protocol="http" bindingInformation="*:21918:localhost" />添加此代码<binding protocol="http" bindingInformation="*:21918:*" /> 。
<site name="project" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="F:\users\Api" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:21918:*" />
</bindings>
</site>
Run Code Online (Sandbox Code Playgroud) 如何在 MVC Asp.net 中每次提交页面时加载图像或文本?因此,基本上我不想在每个页面中重复代码,我想在提交表单(无论在应用程序的哪个位置)时显示图像或文本,和/或更好的方式让用户页面正在加载。
这是_layout.cshtml中的代码
<div class="spinner" style="display:none">
<div class="center-div">
<div class="inner-div">
<div class="loader"></div>
</div>
</div>
</div>
@RenderBody()
Run Code Online (Sandbox Code Playgroud)
这是我编写的用于捕获所有提交按钮的 JavaScript:
<script>
$(document).ready(function () {
$(document.querySelectorAll("button[type=button]")).click(function () {
$('.spinner').css('display', 'block');
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
最后是一个简单的 CSS:
<style>
.center-div {
width: 300px;
height: 300px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -150px;
}
.spinner {
position: fixed;
z-index: 999;
height: 100%;
width: 100%;
top: 0;
left: 0;
background-color: Black;
filter: alpha(opacity=60);
opacity: 0.6;
-moz-opacity: 0.8;
}
.loader {
margin: auto;
border: …Run Code Online (Sandbox Code Playgroud) 我正在使用 ASP.NET core,我读过它已将控制器和 API 合并为一。我已成功地使 JSON 数据正常工作,但是视图无法正常工作。它只是说找不到该页面。我可以查看主页,但是,这是作为 MVC 控制器而不是 API 控制器创建的。
namespace ScheduleWebApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StaffHoursWorkedController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我的行动是
public ActionResult Login(string returnUrl)
{
ViewBag.returnUrl = returnUrl;
return View();
}
Run Code Online (Sandbox Code Playgroud)
在我定义模型的视图中@model OnlineShop.Models.ViewModels.LoginModel,当我使用助手时,@Html.EditorFor(m => m.UserName)我得到了System.NullReferenceException。
我是否真的需要在操作中传递模型的实例,就像return View(new LoginModel())我想使用时一样Html.EditorFor()?
IHttpContextAccessor 给了我:
System.NullReferenceException:“未将对象引用设置为对象的实例。”
我的HomeController:
public class HomeController : BaseController
{
private static IHttpContextAccessor httpContextAccessor;
private static seiren_devContext context;
public HomeController() : base(httpContextAccessor,context)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我的BaseController:
public class BaseController : Controller
{
protected readonly seiren_devContext _context;
protected readonly IHttpContextAccessor _httpContextAccessor;
protected ISession _session => _httpContextAccessor.HttpContext.Session;
public BaseController(IHttpContextAccessor httpContextAccessor, seiren_devContext context)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
}
}
Run Code Online (Sandbox Code Playgroud)
如何让该类以正确的方式HomeController继承?BaseController
我收到这个错误
NoMethodError in UsersController#update
undefined method `update_attributes' for #<User id: 1,
first_name: "Bob", last_name: "Cool",
created_at: "2021-03-10 06:06:13.238325000 +0000",
updated_at: "2021-03-10 06:06:13.238325000 +0000">
Run Code Online (Sandbox Code Playgroud)
我想用这样的形式编辑用户信息;
这是我的users_controller.rb;
NoMethodError in UsersController#update
undefined method `update_attributes' for #<User id: 1,
first_name: "Bob", last_name: "Cool",
created_at: "2021-03-10 06:06:13.238325000 +0000",
updated_at: "2021-03-10 06:06:13.238325000 +0000">
Run Code Online (Sandbox Code Playgroud)
这是我的edit.html.rb
before_action :set_user, only: [:show, :edit, :update]
def update
if @user.update_attributes(user_params)
redirect_to root_path
else
redirect_to edit_user_path(@user),
error: @user.errors.full_messages.join(', ')
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params …Run Code Online (Sandbox Code Playgroud) 我想向我用 Go 编写的命令行应用程序添加一个 GUI,但我遇到了 fyne 和循环依赖项的问题。
考虑这个简单的例子来说明我面临的问题:假设一个按钮触发了我的模型类上的一个耗时的方法(比如获取数据等),并且我希望视图在任务完成时更新。
我首先实现了一个非常幼稚且完全不解耦的解决方案,这显然会遇到 go 编译器引发的循环依赖错误。考虑以下代码:
主程序
package main
import (
"my-gui/gui"
)
func main() {
gui.Init()
}
Run Code Online (Sandbox Code Playgroud)
图形用户界面/gui.go
package gui
import (
"my-gui/model"
//[...] fyne imports
)
var counterLabel *widget.Label
func Init() {
myApp := app.New()
myWindow := myApp.NewWindow("Test")
counterLabel = widget.NewLabel("0")
counterButton := widget.NewButton("Increment", func() {
go model.DoTimeConsumingStuff()
})
content := container.NewVBox(counterLabel, counterButton)
myWindow.SetContent(content)
myWindow.ShowAndRun()
}
func UpdateCounterLabel(value int) {
if counterLabel != nil {
counterLabel.SetText(strconv.Itoa(value))
}
}
Run Code Online (Sandbox Code Playgroud)
模型/model.go
package model
import (
"my-gui/gui" …Run Code Online (Sandbox Code Playgroud) 我实现了一个IUserRepository,在其中实现登录和注册UserRepository;这是我的代码
public async Task<IActionResult> LoginAsync([FromBody] UserCred user)
{
var result = await _signInManager.PasswordSignInAsync(user.username, user.password, user.rememberMe, false);
if (result.Succeeded)
{
var token = _jwtAuthenticationManager.Authenticate(user.username, user.password);
if (token == null)
return Unauthorized();
return Ok(token);
}
return BadRequest();
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
当前上下文中不存在名称“BadRequest”
Ok和 也一样Unauthorized。
但是,当我直接在我的userController. 我想我只能BadRequest在控制器中返回?有什么办法可以克服这个问题,因为如果不是IActionResult,我将如何检查是否results.Succeeded?
此外,这是我的UserController:
[HttpPost]
[AllowAnonymous]
[Route("login")]
public async Task<IActionResult> Login([FromBody] UserCred user)
{
return await _userRepository.LoginAsync(user);
}
Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×5
asp.net-core ×3
c# ×3
asp.net ×2
.net ×1
api ×1
enaml ×1
forms ×1
fyne ×1
go ×1
ipywidgets ×1
itext7 ×1
javascript ×1
pdf ×1
python ×1
ruby ×1
view ×1