English 中文(简体)
示范国家。 IsValid不排除所需财产
原标题:ModelState.IsValid does not exclude required property

试图排除所需财产(密码),使示范州的意图证实该财产,但出于某种原因,即使试图排除该财产,该财产仍然有效。

主计长:

    [Authorize, AcceptVerbs(HttpVerbs.Post)]
    public ActionResult _Edit(int id, [Bind(Exclude = "Password")]FormCollection collection)
    {
        var user = Proxy.GetUser(id);

        TryUpdateModel(user, null, null, new[]{"Password"});

        if(!ModelState.IsValid)
            return PartialView(user);

        Proxy.UpdateUser(user);
    }

观点:

   ...
   <tr>
       <td class="label">
           <label class="row_description" for="Password"><%= S._("Password")%></label>
       </td>
       <td>
           <%= Html.Password("Password", null, new { @class = "row_input" })%>
           <%= Html.ValidationMessage("Password", "*")%>
       </td>
   </tr>

用户(使用数据说明):

[Required]
public string Password { get; set; }

采用VS2008, MVC2, 火灾fox

也许只是tir,可以 see。 感谢任何帮助。

最佳回答

目前,我正与3军委会遇到类似的问题。

尽管[Bind(Exclude = “Password”)] in my Action,ModelState.IsValid 仍不实。

我注意到,<代码>TryUpdateModel(用户,无效,新插图[]{“Pasword”});正在成功地更新该模型;然而,仍然退回不实。 然后,我发现(有些是关于 st流、没有联系的道歉)<代码>。 TryUpdateModel 实际回报<代码>ModelState.IsValid。

因此,这个问题没有<代码>。 TryUpdateModel,但ModelState.IsValid

NB: 这也意味着你不需要两次核实。 您可使用该守则:

if (!TryUpdateModel(user, null, null, new string[]{"Password"}))
    return PartialView(user);

因此,问题似乎是,《示范国家》是否仍然验证被排除于您的<条码>中的特性。

在打电话<代码>TryUpdateModel之前,我得以从ModelState删除这个领域:

ModelState.Remove("Password");

注:最新情况 模型仍要求按照上述代码将财产清单排除在更新之外。

问题回答

我在ASP .NET MVC 2中成功地使用了以下方法:

TryUpdateModel(user);
ModelState.Remove("Password");
if (!ModelState.IsValid) return PartialView(user);

为使TryUpdate从约束性到某些示范性特性,您可建立一个包含性模板,例如:

public interface IUserValidateBindable
{
    string UserId { get; set; }
}

public class User : IUserValidateBindable
{
    [Required]
    public string UserId { get; set; }
    [Required]
    public string Password { get; set; }
}

更新TryUpodateModel电话如下:

TryUpdateModel<IUserValidateBindable>(user);

您可以采用这样的推广方法:

public static bool IsValidExclude(this ModelStateDictionary modelState, params string[] exclude)
{
    foreach (var key in exclude)
    {
        if (modelState.ContainsKey(key))
            modelState.Remove(key);
    }

    return modelState.All(m => m.Value.Errors.Count == 0);
}

接着只是呼吁:

var result = ModelState.IsValidExclude("Password");

愿各位替代

TryUpdateModel(user, null, null, new[]{"Password"});

iii

TryUpdateModel(user, null, null, new string[] {"Password"});

Because it might be confusing which overload for TryUpdateModel is using. Just saying...

我成功使用<代码>[Bind(Exclude = “Property”)]和ModelState.Remove(“Property”),并且与药店合作。

在我的案件中,我面临模范国家的错误。 转口号是,它不为我工作,而是在网皮上打字。

“entergraph

接着,我添加了模型<代码>ModelState.Remove(“模型描述”);,即我把模型定义为参数,然后我使用参数名称,然后对我进行模仿:

“enterography

Seems such as I m responseing太晚,但我也面临同样的问题。

查阅<代码>ModelState.Keys Collection。 钥匙可以是modelObjectName.Pasword,其他模型性质相同。

因此,在这一案例中,<代码>ModelState.Remove(“Pasword”)将不可行。 页: 1

希望解决某人的问题:





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签