我试图以文件/ id格式获取我的URL .我猜我的控制器中应该有两个Index方法,一个带参数,一个带不带.但我在下面的浏览器中收到此错误消息.
无论如何这里是我的控制器方法:
public ActionResult Index()
{
return Content("Index ");
}
public ActionResult Index(int id)
{
File file = fileRepository.GetFile(id);
if (file == null) return Content("Not Found");
else return Content(file.FileID.ToString());
}
Run Code Online (Sandbox Code Playgroud)
更新:完成添加路线.谢谢Jeff
c# model-view-controller asp.net-mvc controller asp.net-mvc-routing
有一些旧代码在某种情况下会修改params.我相信它之前有效(不是100%).我们现在将params设置为nil,无论条件是否满足.
罪魁祸首是在条件之内,我执行了params = tmp.dup.即使条件为假,也会导致更新操作出错.
我能够通过最小的测试重新创建
(Rails 2.3.5)
rails bug;
cd bug;
script/generate scaffold bug name:string;
rake db:create;
rake db:migrate;
Run Code Online (Sandbox Code Playgroud)
编辑apps/controllers/bugs_controller.rb添加到更新操作的开头
l_p = params.dup
if (false)
params = l_p.dup # NOT REACHED
end
Run Code Online (Sandbox Code Playgroud)
脚本/服务器WEBrick -p 5001
浏览到http:// localhost:5001/bugs 创建一个新的bug编辑错误提交
文档对此不是很清楚.有人可以阐明这两者之间的差异吗?
@After - 在对此Controller的每个动作调用之后执行使用@After注释注释的方法.
@Finally - 在为此Controller应用每个操作结果后,将执行使用@Finally注释注释的方法.
当@After工作时,使用@Finally会不会影响性能?
下面是我发送到我的ASP.NET MVC2控制器的POST请求(使用Firebug Net面板)的屏幕截图.

这是接收POST请求的控制器/操作方法:
public ActionResult Search(int[] skill, int[] discipline, int[] education, int[] marketsector){ ... }
Run Code Online (Sandbox Code Playgroud)
POSTs参数在我看来是正确的形式,但action方法接收四个空参数.你能否告诉我在服务器端方法签名应该是什么样的?或者将这四个整数数组发布到我的MVC2控制器的更好方法是什么?
当我从控制器渲染视图时,传递给视图的变量位于视图中呈现的所有模板的范围内,包括嵌套在多个深层的模板.
但是,当我从控制器渲染模板时(如在ajax样式中),只有初始模板似乎可以访问变量,并且嵌套模板调用不共享此信息.例:
Views & Templates:
a.gsp
_b.gsp
_c.gsp
_one.gsp
_two.gsp
_three.gsp
//PasserController.groovy
class PasserController{
def v = {
render(view:"a", model:[time:System.currentTimeMillis()])
}
def t = {
render(template:"one", model:[time:System.currentTimeMillis()])
}
}
//a.gsp
A Time is: ${time}<br />
<g:render template="b" />
//_b.gsp
B Time is: ${time}<br />
<g:render template="c" />
//_c.gsp
C Time is: ${time}<br />
//_one.gsp
One Time is: ${time}<br />
<g:render template="two" />
//_two.gsp
Two Time is: ${time}<br />
<g:render template="three" />
//_three.gsp
Three Time is: ${time}<br />
Run Code Online (Sandbox Code Playgroud)
输出如下:
For passer/v …Run Code Online (Sandbox Code Playgroud) 好吧,伙计们,需要一些帮助!
我正在使用asp.net mvc3 razor(我相当新,但做了很多网页表单)
好的,这个问题
我的问题围绕提交视图.我有一个非常复杂的模型,我的视图基于此(强类型).
我想将模型返回到控制器的HttpPost方法中的参数.基本上做:
public ActionResult Personal()
{
DataModel dataModel = new DataModel();
FormModel model = new FormModel();
model.candidateModel = dataModel.candidateModel;
model.lookupModel = new LookupModel();
return View(model);
}
[HttpPost]
public ActionResult Personal(FormModel formModel)
{
if (ModelState.IsValid)
{
//stuff
}
return View(formModel);
}
Run Code Online (Sandbox Code Playgroud)
现在......
我无法在post方法的formModel参数中获取值.
这工作(意味着我可以看到值)但是很繁琐,因为我必须准确地写出每个字段在字符串中的位置:
@Html.TextBox("formModel.candidateModel.tblApplicant.FirstName", Model.candidateModel.tblApplicant.FirstName)
Run Code Online (Sandbox Code Playgroud)
它呈现如下:
<input name="formModel.candidateModel.tblApplicant.FirstName" id="formModel_candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/>
Run Code Online (Sandbox Code Playgroud)
这不起作用:
@Html.TextBoxFor(c => c.candidateModel.tblApplicant.FirstName)
Run Code Online (Sandbox Code Playgroud)
它呈现如下:
<input name="candidateModel.tblApplicant.FirstName" id="candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/>
Run Code Online (Sandbox Code Playgroud)
现在我假设问题在于id的差异
所以请回答我:
快速澄清:当我说textboxfor无法正常工作时,它会在我获取表单时获取值.所以他们填写,但在POST /提交,我无法在参数中的formModel中看到它们.
另一方面说明:
没有html助手工作,这就是问题所在.它们也没有出现在模型状态中.
谢谢大家的帮助
答案: …
我有一个'run'对象,可以在其中保存许多'层'.运行接受图层的嵌套属性,但是在发生各种不良事件之前,rails无法确保嵌套对象的参数有效.我正在尝试检查任何层上的'deposition_source_id'参数是否为空.我怎样才能遍历图层?
该行可以从一个层或任何层获取参数."0"指定图层.
params[:run][:layers_attributes]["0"][:deposition_source_id]
我该如何检查每一层?这些是参数:
=> {"utf8"=>"?",
"_method"=>"put",
"authenticity_token"=>"T+X6sSda5vV19hpMZEAdf5RWSKPhJrm/q9+NXxTC5G8=",
"run"=>
{"number"=>"31310.0",
"start_time_string"=>"08/08/2012 at 11:08 AM",
"system_id"=>"4",
"technician_id"=>"4",
"duration"=>"",
"base_pressure"=>"",
"platters"=>"Apples",
"overcoats_string"=>"",
"planetary"=>"",
"layers_attributes"=>
{"0"=>
{"deposition_source_id"=>"",
"material_id"=>"60",
"lot_id"=>"118",
"thickness_goal"=>"32",
"measured_thickness"=>"3",
"tooling_factor"=>"",
"sensor_number"=>"",
"xtal_life"=>"",
"release_agent"=>"0",
"_destroy"=>"false",
"id"=>"3401"}}},
"commit"=>"Update Run",
"action"=>"update",
"controller"=>"runs",
"id"=>"2319"}
Run Code Online (Sandbox Code Playgroud) 如果使用控制台在Symfony2中为实体创建CRUD代码,则最终会得到一个非常基本的删除功能.
这个功能精简高效,但没有提供"你确定吗?" - 确认.如果要删除的实体存在,则会立即删除.
有没有人有最简单的添加用户确认方法的建议?
到目前为止,我一直在使用:
虽然Symfony2没有内置选项,但似乎有点奇怪.谁有更好的想法?
我的会话控制器中有一个变量.
session[:facebook_profile_id] = @user_info['id']
Run Code Online (Sandbox Code Playgroud)
@user_info ['id']是一个int.示例:123
然后,我在主控制器中使用该会话变量从数据库中获取配置文件对象.
def show
@facebook_profile = FacebookProfile.find_by_facebook_id(session[:facebook_profile_id])
end
Run Code Online (Sandbox Code Playgroud)
使用会话变量找到新对象并在我的应用程序中使用,因此我的rspec测试在没有它的情况下失败.
这是我的工作室FacebookProfile:
FactoryGirl.define do
factory :facebook_profile do |f|
f.facebook_id 123
end
end
Run Code Online (Sandbox Code Playgroud)
在我对应用程序的规范测试中,我在每次测试之前创建了Factory实例:
FactoryGirl.create(:facebook_profile).should be_valid
Run Code Online (Sandbox Code Playgroud)
如何在我的规范测试中设置session [:facebook_profile_id]变量,以便@facebook_profile的查找不会失败?
我试过抄袭,但无法让它发挥作用.另外,我一直在我的功能规范中尝试这个.我应该在控制器规范中这样做吗?
我有以下控制器方法:
public ActionResult GetResults(string viewToReturn, int resultsPerPage,
string classification = null, string sessionId = null, int? lastId= null)
{
...
}
Run Code Online (Sandbox Code Playgroud)
通过以下url调用上面的方法:
http://localhost:63455/Home/GetResults?viewToReturn=grid&resultsPerPage=30
Run Code Online (Sandbox Code Playgroud)
导致此消息抛出异常:
在控制器'MyWebSite.Controllers.HomeController'上找不到公共操作方法'GetResults'.
这里是RegisterRoutes:
......
routes.MapRoute("Home", "home/{action}/{*qualifier}",
new { controller = "Home", action = "Index", qualifier = UrlParameter.Optional });
......
routes.MapRoute("SearchTitle", "{*path}",
new { controller = "Home", action = "SearchTitle", path = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误,如何解决?谢谢!