我环顾四周,找不到简单的解决方案。我已经尝试过@GetUserName,但它不起作用。我已经尝试过@{GetUserName,但它不起作用。必须有一种从RazorView引擎调用方法的简单方法。它在foreach循环中。我需要GetUserName(item.userID)下面的代码在我的Controller中:[ChildActionOnly]publicstringGetUserName(intuserID){ProPit_Useruser=db.ProPit_User.Find(userID);returnuser.username;} 最佳答案
你知道为什么会这样吗:publicstructUserNameAndPassword{publicstringusername;publicstringpassword;}[HttpPost]publicIActionResultCreate([FromBody]UserNameAndPasswordusernameAndPassword){Console.WriteLine(usernameAndPassword);if(this.AuthenticationService.IsValidUserAndPasswordCombination(usernameAndPassword.u
这里我从数据库中获取值并将其显示在输入字段中并且从数据库中获取的值是1。然后我将输入字段值更改为2并在点击操作中将该值传递给Controller但在Controller部分,我得到了旧值1forqty。但是我需要中的更新值2数量publicActionResultUpdate(stringid,stringproductid,intqty,decimalunitrate){if(ModelState.IsValid){int_records=UpdatePrice(id,productid,qty,unitrate);if(_records>0){returnRedirectToA
我已尝试完成这项工作,并进行了多次google/stackoverflow搜索,但一点运气都没有。我有一个简单的模型:publicclassMovieModel{publicstringId{get;set;}[Required][StringLength(100)]publicstringName{get;set;}}Controller中的一个方法://POST:api/MoviespublicIHttpActionResultPost([FromBody]MovieModelmovieModel){if(ModelState.IsValid){//Code}}和一个测试方法(是一
我想将[FirstTime]属性放在Controller函数之上,然后创建一个FirstTimeAttribute,它具有一些逻辑来检查用户是否输入了他的名字,并重定向如果他还没有,请他到/Home/FirstTime。所以与其这样做:publicActionResultIndex(){//Somemajorlogichereif(...)returnRedirectToAction("FirstTime","Home");returnView();}我会这样做:[FirstTime]publicActionResultIndex(){returnView();}这可能吗?
我试图将我的ASP.NETCore路由的Controller限制到某个命名空间。在以前版本的ASP.NETMVC中,有一个重载在添加路由时提供了一个string[]namespaces参数。这在ASP.NETMVC6中是缺失的。所以在谷歌搜索之后,我试着玩弄类似的东西app.UseMvc(routes=>{vardataTokens=newRouteValueDictionary{{"Namespaces",new[]{"ProjectA.SomeNamespace.Controllers"}}};routes.MapRoute(name:"default",template:"{co
我有两个名称相同但方法签名不同的Controller操作。它们看起来像这样:////GET:/Stationery/5?asHtml=true[AcceptVerbs(HttpVerbs.Get)]publicContentResultShow(intid,boolasHtml){if(!asHtml)RedirectToAction("Show",id);varresult=Stationery.Load(id);returnContent(result.GetHtml());}////GET:/Stationery/5[AcceptVerbs(HttpVerbs.Get)]publ
如果是这样,我应该如何传递参数?匹配枚举名称的字符串可以吗?如果我要传递匹配枚举项的下拉框,这会很方便。使用thisanswer中提供的解决方案会很有用如果我在提交回数据时可以轻松地绑定(bind)到枚举。 最佳答案 是的,当有这样的Controller时:enumMyAction{Lalala}publicActionResultIndex(MyActionaction){...}您只需执行Index/Lalala,一切正常。如果您需要更复杂的绑定(bind)(比如将某个字符串值映射到一个复杂的类),请使用类似StructureM
在FrameworkWebAPI2中,我有一个如下所示的Controller:[Route("create-license/{licenseKey}")]publicasyncTaskCreateLicenseAsync(stringlicenseKey,CreateLicenseRequestlicense){try{//...controller-ystuffreturnawait_service.DoSomethingAsync(license).ConfigureAwait(false);}catch(Exceptione){_logger.Error(e);conststri
我目前有一个带有LINQ语句的Controller,我正在将数据从中传递到我的View。我正试图找到一种更有效和更好的编码方法来做到这一点。我的家庭Controller声明如下。VarMeltFurnace1=(fromitemindb.tbl_dppITHrwhereitem.ProductionHour>=StartShift&&item.ProductionHourx.Furnace1Total),ViewData["Furnace1Total"]=Melt.Furnace1;在我看来,我然后引用ViewData来显示它。使用@modeldynamic现在我在Index方法中有很