- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
目标是在主下拉列表的项目更改时更改辅助下拉列表的项目。
带着无比自信和乐观的想法,我想出了这个简单的代码:
型号:
public class ChoixModel
{
public INSC_Inscription inscription { get; set; }
public List<Ecole> ecoles { get; set; }
public List<Programme> programmes { get; set; }
public int? ecoleChoi1 { get; set; }
public int? programmeChoi1 { get; set; }
}
Controller :
public ActionResult Phase1(int idInscrip)
{
inscriptionPrimaireEntities db = new inscriptionPrimaireEntities();
ChoixModel model = new ChoixModel();
if (model.ecoleChoi1 == null)
model.ecoleChoi1 = db.Ecole.FirstOrDefault()?.id;
model.ecoles = db.Ecole.ToList();
model.programmes = db.Programme.Where(t => t.FK_Ecole == model.ecoleChoi1).ToList();
return View(model);
}
[HttpPost]
public ActionResult Phase1(ChoixModel model, string submiter)
{
inscriptionPrimaireEntities db = new inscriptionPrimaireEntities();
if (!string.IsNullOrWhiteSpace(submiter))
{
if (model.ecoleChoi1 != null)
model.programmeChoi1 = db.Programme.FirstOrDefault(t => t.id == model.inscription.Programme1)?.id;
}
return View(model);
}
View :
@using System.Web.UI.WebControls
@using inscriptionPrimaire.Models
@model ChoixModel
@{
ViewBag.Title = "Phase1";
Layout = "~/Views/Shared/_Layout.cshtml";
SelectList selectEcole = new SelectList(Model.ecoles, "id", "NomAffiche");
SelectList selectPrograme = new SelectList(Model.programmes, "id","NomAffiche");
}
<form method="post" action="@Url.Action("Phase1", "Choix")" id="ViewModelForm">
<br /><br />
<table>
<tr>
<td style="min-width: 152px;">
@Html.DropDownListFor(t => t.ecoleChoi1, selectEcole, new { @class = "form-control", style = "min-width: 150px;", onchange = "this.form.submit();" })
</td>
<td style="min-width: 152px;">
@Html.DropDownListFor(t => t.programmeChoi1, selectPrograme, new { @class = "form-control", style = "min-width: 150px;" })
</td>
</tr>
</table>
</form>
当我单击更改主下拉列表 Ecole
的选定项时发生错误。我怀疑这与我如何使用提交有关:
@Html.DropDownListFor(t => t.ecoleChoi1, selectEcole, new { @class = "form-control", style = "min-width: 150px;", onchange = "this.form.submit();" })
预期的行为是,当我更改下拉列表 Ecole
的选定项目时,它将用 提供的实际程序填充下拉列表
。 Programme
选择 Ecole
相反,我有一个错误,一个调试甚至无法捕获的错误,它只显示在浏览器中:
an item with the same key has already been added
所以我去查看我的数据库是否有任何重复...两个表中甚至没有一行包含与前一个相同的任何内容。
我被要求显示完整的堆栈跟踪(它是法语):
Erreur du serveur dans l'application '/'.
Un élément avec la même clé a déjà été ajouté.
Description : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code.
Détails de l'exception: System.ArgumentException: Un élément avec la même clé a déjà été ajouté.
Erreur source:
Une exception non gérée s'est produite lors de l'exécution de la requête Web actuelle. Les informations relatives à l'origine et l'emplacement de l'exception peuvent être identifiées en utilisant la trace de la pile d'exception ci-dessous.
Trace de la pile:
[ArgumentException: Un élément avec la même clé a déjà été ajouté.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12534430
System.Collections.Generic.CollectionExtensions.ToDictionaryFast(TValue[] array, Func`2 keySelector, IEqualityComparer`1 comparer) +116
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +136
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +176
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1197
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330
System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +17
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +377
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1197
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +331
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343
System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
最佳答案
问题是您使用的是复杂的数据库实体模型,模型绑定(bind)器无法从表单数据中正确重建。
我建议您避免往返实体,只接受您需要的值。您可以通过从 HttpPost
方法中删除模型并直接接受值来完成此操作:
[HttpPost]
public ActionResult Phase1(int ecoleChoi1 , int programmeChoi1, string submiter)
{
//...
//You can then reconstruct the model if necessary and send back
ChoixModel model = new ChoixModel();
//...
return View(model);
}
在 Razor View 上:
@Html.DropDownList("ecoleChoi1 ", selectEcole, new { @class = "form-control", style = "min-width: 150px;", onchange = "this.form.submit();" })
@Html.DropDownList("programmeChoi1", selectPrograme, new { @class = "form-control", style = "min-width: 150px;" })
如果调试它,您会发现模型绑定(bind)器从下拉列表中传回值。
或者,由于这两个列表不是真正的可编辑字段而是选择字段,您可以将它们从模型中移除并改为将它们传递到 ViewBag 中。这样您就可以继续接受您的模型。
关于c# - 下拉列表一直告诉我有重复,但没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49456485/
我有一个在 Android 市场上相当流行的应用程序,它允许数以万计的用户按下一个按钮并向它发出语音命令。然后我就可以做很多不同的事情,比如给他们提供当前的天气预报等等...... 无论如何,我的应用
令人惊讶的是,标题基本上解释了它。我们有一个我们的客户制作的页面,我们正在重新创建该页面。 页面高度会一直增加,直到(我假设是这样)浏览器达到它的极限。我已经尝试过 Firebug 和 W3 验证器,
我是 react-native 的新手,试图创建我自己的组件,但它一直显示一个空屏幕。 这是我的组件代码 class BoxComponent extends Component { cons
我正在为我的 PHP 元素创建一个非常简单的博客,但遇到了一个简单的问题。我无法让我的页眉图像一直 float 。我有一个横幅,左边有一些文字,我有一个 1px 的切片,在可以选择的任何分辨率的宽度上
为什么我可以在另一个 Controller 的 View 中访问一个 Controller 的辅助方法?有没有办法在不破解/修补 Rails 的情况下禁用它? 最佳答案 @George Schreib
我正在使用带有最新 ADT 插件的 Eclipse Kepler SP2。每隔一分钟 Eclipse 就会说“为 Android 4.4.2 加载数据”并阻止我想做的一切。我在不同的文件夹中有几个 E
我是一名优秀的程序员,十分优秀!