简体   繁体   English

DropDownListFor的选定值可用于ViewModel,但不能用于整数

[英]DropDownListFor`s Selected value works with ViewModel but not integer

When I select a template in my dropdownlist and press the OK button to make a form post the DeleteTemplateListViewModel object is created and filled with the current templateId and the ModelState is Valid. 当我在下拉列表中选择模板并按OK按钮以创建表单时,将创建DeleteTemplateListViewModel对象,并用当前templateId填充,并且ModelState为有效。

When I select the default value "Select a template" in my dropdownlist and press the OK button to make a form post the DeleteTemplateListViewModel object is created and and the ModelState is InValid. 当我在下拉列表中选择默认值“选择模板”并按“确定”按钮以创建一个窗体后,创建DeleteTemplateListViewModel对象,并且ModelState为InValid。

So far so good. 到现在为止还挺好。

But when I change the Delete action to accept an integer which is the selectedTemplateId the ModelState is Valid for a selected templateId. 但是,当我更改Delete动作以接受为selectedTemplateId的整数时,ModelState对所选templateId有效。 When I select the default value "Select a template" defined in the DropDownListFor I get in my output window this exception: 当我选择在DropDownListFor中定义的默认值“选择模板”时,出现以下异常:

A first chance exception of type 'System.ArgumentException' occurred in System.Web.Mvc.dll System.Web.Mvc.dll中发生类型为'System.ArgumentException'的第一次机会异常

How can I make it work with so the ModelState is false/Invalid for the default value using an integer as action parameter instead of a viewModel ? 如何使用整数作为操作参数而不是viewModel,使ModelState为false /对于默认值无效,我如何使用它?

@model ITMS.Web.Models.DeleteTemplateListViewModel

@using (Html.BeginForm("Delete", "Template"))
{ 
    @Html.ValidationSummary(false)      
    @Html.DropDownListFor(x => x.SelectedTemplateId, 
                               new SelectList(Model.DisplayList, "TemplateId", "Name"),
                               "Select a template", 
                               new { @class = "listviewmodel" })
}


public class DeleteTemplateListViewModel
{
    [Required(ErrorMessage = "No template selected.")]
    public int SelectedTemplateId { get; set; }
    public string Name { get; set; }
    public IEnumerable<TemplateViewModel> DisplayList { get; set; }
}

WORKS 作品

[HttpPost]
public ActionResult Delete(DeleteTemplateListViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        int templateId = viewModel.SelectedTemplateId;
        _templateDataProvider.DeleteTemplate(templateId);
        return new JsonNetResult(new { success = true, templateId = templateId });
    }
    return LoadDeleteTemplates();
}

DOES NOT WORK 不起作用

[HttpPost]
public ActionResult Delete(int selectedTemplateId)
{
    if (ModelState.IsValid)
    {
        _templateDataProvider.DeleteTemplate(selectedTemplateId);
        return new JsonNetResult(new { success = true, templateId = selectedTemplateId});
    }
    return LoadDeleteTemplates();
}

Well, an int can't really get a non-value. 好吧,一个int不能真正获得非值。 Change your Delete method to accept and int? 更改您的Delete方法以接受并int? .

I don't know what kind of model validation MVC can do an an int? 我不知道MVC可以进行哪种模型验证? with no additional data, but you should check see if it's null or not. 没有其他数据,但是您应该检查它是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM