我的代码是:
cliente.Country = new List<dic_paises>();
dic_paises C = ((from data in db.dic_paises where data.id == 61 select data).SingleOrDefault());
cliente.Country.Add(new dic_paises() { id = C.id, nome = C.nome });
return View();
我想要做的是,我有一个表格客户端和一个表格国家/地区,现在我想在视图中显示客户端而不是显示存储在客户端中的国家/地区的ID我要显示国家/地区名称,我很难在mvc中完成它,因为我是mvc的新手。尝试使用视图模型,但没有得到预期的结果。
public partial class cliente
{
public int id { get; set; }
[StringLength(1024)]
public string nome { get; set; }
[StringLength(1024)]
public string apelido { get; set; }
[StringLength(1024)]
public string email { get; set; }
public List<dic_paises> Country { get; set; }
}
这是客户端模型,这里是国家/地区型号:
public partial class dic_paises
{
public int id { get; set; }
[Required]
[StringLength(1024)]
public string nome { get; set; }
}
这就是我在我的控制器中所做的,在youtube上看到了一个视频...
cliente cliente = db.clientes.Find(id);
if (cliente == null)
{
return HttpNotFound();
}
cliente.Country = new List<dic_paises>();
dic_paises C = ((from data in db.dic_paises where data.id == 61 select data).SingleOrDefault());
cliente.Country.Add(new dic_paises() { id = C.id, nome = C.nome });
return View();
请尽力帮助......
这是错误......
[SqlException(0x80131904):列名无效&#39; cliente_id&#39;。]
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action1 wrapCloseInAction) +2418094
1 wrapCloseInAction)+5694468
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+285
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&amp; dataReady)+3731
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()+58
System.Data.SqlClient.SqlDataReader.get_MetaData()+89
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)+379
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task&amp; task,Boolean asyncWrite,SqlDataReader ds,Boolean describeParameterEncryptionRequest)+2064
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375
1 c)+14
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +240
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext
System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func
3操作,TInterceptionContext interceptionContext,Action3 executing, Action
3执行)+72
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)+402
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)+166
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)+12
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)+36[EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常。]
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)+103
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context,ObjectParameterCollection parameterValues)+758
System.Data.Entity.Core.Objects。&lt;&gt; c__DisplayClass7.b__6()+90
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288
1次操作)+190
System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +154
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable
1 forMergeOption)+283 System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +15 System.Data.Entity.Internal.LazyEnumerator
1.MoveNext()+45 System.Linq.Enumerable.SingleOrDefault(IEnumerable1 source) +121 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2(IEnumerable
1序列)+40 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable1 query, Expression queryRoot) +59 System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +114 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +116 System.Linq.Queryable.SingleOrDefault(IQueryable
1 source)+211 WebApplication2.Controllers.clientesController.Details(Nullable1 id) in C:\Users\dani1\Documents\Visual Studio 2015\Projects\WebApplication2\WebApplication2\Controllers\clientesController.cs:56 lambda_method(Closure , ControllerBase , Object[] ) +107 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2个参数)+157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary2 parameters) +27 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22 System.Web.Mvc.Async.WrappedAsyncResult
2.CallEndDelegate(IAsyncResult asyncResult)+29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult)+29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult)+22 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult)+29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9723757 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
答案 0 :(得分:0)
情况就这样解决了......
我猜这就是他们所说的视图模型
public class ClienteCountry
{
public cliente cli { get; set; }
public dic_paises country { get; set; }
}
控制器代码......
cliente ccc = (from data in db.clientes where data.id == id select data).FirstOrDefault();
dic_paises ppp = (from data in db.dic_paises where data.id == ccc.pais select data).FirstOrDefault();
ClienteCountry cc = new ClienteCountry();
cc.cli = ccc;
cc.country = ppp;
ViewBag.id = id;
return View(cc);
其中id是通过视图传递的参数...
在视图中我把它......
@model WebApplication2.Models.ClienteCountry
然后我能够使用我的属性......
@Html.DisplayNameFor(model => model.cli.nome)
@Html.DisplayFor(model => model.cli.nome)
@Html.DisplayNameFor(model => model.cli.apelido)
@Html.DisplayFor(model => model.cli.apelido)
@Html.DisplayName("Country")
@Html.DisplayFor(model => model.country.nome)
无论如何,感谢你们所有人,......