在我的控制器的动作中,我有以下代码:
public ActionResult GridAction(string id)
{
if (String.IsNullOrEmpty(id))
{
// add errors to the errors collection and then return the view saying that you cannot select the dropdownlist value with the "Please Select" option
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
更新:
if (String.IsNullOrEmpty(id))
{
// add error
ModelState.AddModelError("GridActionDropDownList", "Please select an option");
return RedirectToAction("Orders");
}
Run Code Online (Sandbox Code Playgroud)
更新2:
这是我更新的代码:
@Html.DropDownListFor(x => x.SelectedGridAction, Model.GridActions,"Please Select")
@Html.ValidationMessageFor(x => x.SelectedGridAction)
Run Code Online (Sandbox Code Playgroud)
该模型如下所示:
public class MyInvoicesViewModel
{
private List<SelectListItem> _gridActions;
public int CurrentGridAction { get; set; }
[Required(ErrorMessage = …Run Code Online (Sandbox Code Playgroud) 我有一个win表单应用程序,其中列表框显示方法(按属性).我试图动态调用线程中的方法,使用反射从列表框的选定值获取方法信息.但是,当调用Methodinfo.Invoke时,我得到了这个内部异常"非静态方法需要一个目标C#".
这是我的代码(请记住,我仍然是c#和编程的新手.)
private void PopulateComboBox()
{//Populates list box by getting methods with declared attributes
MethodInfo[] methods = typeof(MainForm).GetMethods();
MyToken token = null;
List<KeyValuePair<String, MethodInfo>> items =
new List<KeyValuePair<string, MethodInfo>>();
foreach (MethodInfo method in methods)
{
token = Attribute.GetCustomAttribute(method,
typeof(MyToken), false) as MyToken;
if (token == null)
continue;
items.Add(new KeyValuePair<String, MethodInfo>(
token.DisplayName, method));
}
testListBox.DataSource = items;
testListBox.DisplayMember = "Key";
testListBox.ValueMember = "Value";
}
public void GetTest()
{//The next two methods handle selected value of the listbox and invoke …Run Code Online (Sandbox Code Playgroud) 当我用机架和美洲狮开始我的Sinatra应用程序时出错.我的config.ru文件看起来像这样:
#\ -s puma
require './controller/main.rb'
run Sinatra::Application
Run Code Online (Sandbox Code Playgroud)
因此,当我现在使用rackup时,我收到此错误:
/home/username/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.5.2/lib/rack/handler.rb:76:in"require":无法加载此类文件 - 机架/处理程序/ puma(LoadError)
我用ruby 1.9.3p392(2013-02-22修订版39386)[i686-linux]
我的第一个想法是我忘了安装美洲狮,或者美洲狮在某种程度上被打破了.所以我尝试过:
puma -v
puma version 2.0.1
Run Code Online (Sandbox Code Playgroud)
我直接用ruby开始它:
ruby controller/main.rb
Puma 2.0.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
Run Code Online (Sandbox Code Playgroud)
我发现了这个美洲狮问题,但我找不到真正的解决方案.
为什么要发生这种情况?
我怎样才能解决这个问题?
我有一条/开始无限循环的路线(从技术上讲,直到 Websocket 断开连接,但在这个简化的示例中,它确实是无限循环)。如何在关机时停止此循环:
from fastapi import FastAPI
import asyncio
app = FastAPI()
running = True
@app.on_event("shutdown")
def shutdown_event():
global running
running = False
@app.get("/")
async def index():
while running:
await asyncio.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)
根据文档, @app.on_event("shutdown")应该在关闭期间调用,但怀疑它的调用类似于生命周期事件,该事件在一切完成后调用,在这种情况下这是一个死锁。
去测试:
uvicorn module.filename:app --host 0.0.0.0CTRL+C)你会看到它永远挂起,因为 running 永远不会设置为 false,因为shutdown_event没有被调用。(是的,您可以按 强制关机CTRL+C)
SqlConnection cn = new SqlConnection("server=localhost;initial catalog=newmits;trusted_connection=true");
cn.Open();
string a = string.Format("select * from upnotice where show like '{0}' ,%t");
SqlDataAdapter adp1 = new SqlDataAdapter(a, cn);
DataSet ds1 = new DataSet();
adp1.Fill(ds1);
GridView1.DataSource = ds1;
GridView1.DataBind();
Run Code Online (Sandbox Code Playgroud)
当我在没有条件的情况下尝试它可以工作但是在它无法工作的地方请帮助我
我有一个类型列表,其中包含(string,string,string,string,int,DateTime,DateTime)字符串组合中的字符总数等于28.
我在一个文件中读到填充列表,该列表将在列表中以19,000,000(一千九百万)个这些对象结束.
我在文件中读到并添加到列表中
public void ReadDocGrabValues(string fileREAD)
{
using (var file = new StreamReader(fileREAD))
{
var j = file.ReadLine();
while (j != null)
{
mylist.Add(new IDandAGE(j.Substring(0, 15), j.Substring(16, 1), j.Substring(18, 6), j.Substring(25, 6), 0, DateTime.Today, DateTime.Today));
j = file.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪个应该不是问题.然后,我遍历整个列表,从两个字符串中计算出DateTime对象.
public void ConvertYOBDOI()
{
foreach (IDandAGE x in mylist)
{
string IssueDate = (x.StringDOD.Substring(0,4) + "-" + x.StringDOD.Substring(4,2) + "-01");
string BirthDate = (x.StringYOB.Substring(0,4) + "-" + x.StringYOB.Substring(4,2) + "-01");
x.DeathDate= DateTime.Parse(DeathDate);
x.YearOfBirth = DateTime.Parse(BirthDate);
}
} …Run Code Online (Sandbox Code Playgroud) 我在Sinatra项目中使用RSpec.我正在测试一个运行after_find钩子的模型来初始化一些值.我需要在每次运行之前擦除数据库,经过一些搜索后,我的规范帮助文件如下所示:
require "pry"
require "factory_girl_rails"
require "rspec-rails"
FactoryGirl.find_definitions
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.use_transactional_fixtures = true
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
undefined method `use_transactional_fixtures=' for #<RSpec::Core::Configuration:0x007fc1c89397e8> (NoMethodError)`
Run Code Online (Sandbox Code Playgroud)
我查阅了RSpec :: Core :: Configuration的文档,唯一提到的use_transactional_fixtures=是它特别指出它是特定于rails的.我安装了'rspec-rails'宝石,所以我不知道它为什么不使用它.我甚至在spec帮助文件中要求它.
我最初的想法是创建一个运行db:drop,db:create和db:migrate然后测试的Rakefile,但我希望有一个稍微简单的方法 use_transactional_fixtures=
尝试调用我的ToString()方法时,我收到上面的错误消息.我不确定问题是什么.这是我的代码:
public override string ToString()
{
return string.Format("{0} Pizzas @ {1:C}: {2:C}\n" +
"{3} Cokes @ {4:C} {5:C}\n" +
"Order Amount: {6:C}\n" +
"Sales Tax: {7:C}\n" +
"Amount Due: {8:C}\n" +
"Amount Paid: {9:C}\n" +
"Change Due: {10:C}", numberOfPizzas, PIZZA_PRICE +
totalCostOfPizza, numberOfCokes, COKE_PRICE, totalCostOfCoke +
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid, +
changeDue);
}
Run Code Online (Sandbox Code Playgroud) 我和我的朋友们正在玩 C++ 语言。在这样做的过程中,我们遇到了一些我们无法理解的事情。
这是代码:
#include <vector>
#include <iostream>
void print(std::vector<char> const &input)
{
std::cout << input.size();
for (int i = 0; i < input.size(); i++)
{
std::cout << input.at(i) << " - ";
}
}
int main()
{
char cha = 'A';
char chb = 'B';
char * pcha = &cha;
char * pchb = &chb;
try
{
std::vector<char> a = {pcha, pchb};
//std::vector<char> a = {pchb, pcha};
print(a);
}
catch(std::exception e)
{
std::cout << e.what();
}
}
Run Code Online (Sandbox Code Playgroud)
此代码的输出:
一种 …