English 中文(简体)
避免输入元件价值重新贴在服务器上,但允许MVC进行验证
原标题:Avoiding input element value posted back to server but allow MVC validation to occur

我有投入、类型文字、内容,正在通过MVC3验证客户,验证这些要素,而我则希望在员额出现时没有向服务器发送输入值。

我有两个实体:一个“File”实体和一个“Company”实体,共有1到1个关系。 档案实体有一家外国钥匙公司。

因此,如果你看一看他们的名字和背后特性:档案.Company.Code或 文件:Company_Code。

我想避免将投入价值退回服务器的原因是,当请求送达服务器时,我只想把价值约束给我的“零”实体。 由于它也获得了“File.Company.Code”的价值,因此,它也正在对价值与我想要避免的File.Company.Code公司目标挂钩。

The input element is :

    <input name="File.Company.Code" id="File_Company_Code" type="text" data-val-required="Se requiere un c&#243;digo de cliente." data-val="true" value=""/>

    And the span element:
    <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for=" File.Company.Code "/>

I’ve tried:
-Changing the input name and span data-valmsg-for attributes using jquery. But I think that after doing this I may need to rebind the validators??

Any suggestions? (I hope to have explained myself clearly if not let me know.)
Thanks

UPDATE 1 **
Thanks to AFinkelstein sugestion which put me on the right track I updated my domain model such as:

public class FileModel {

        public File File {
            get {
                return this.file;
            }
        }

        *** ADDED this which helped me solve the problem ***
        public Company Company {
            get {
                return this.file.Company;
            }
        }
    }

我认为,不要这样做:

 @Html.TextboxFor(model => model.File.Company.Code)  
  @Html.ValidationMessageFor(model => model.File.Company.Code)

我现在:

  @Html.TextboxFor(model => model.Company.Code)  
  @Html.ValidationMessageFor(model => model.Company.Code)

这样,产生的名称和特征就具有价值:公司。 《刑法》和《刑法》公司(Code)在前列“File”。 当我接收服务器上的这个职位并将这些数值与文档标的挂钩时:

 FileModel fileModel = new FileModel();
            try {

                TryUpdateModel(fileModel.File, "File");

因为它没有获得“File.Company”的价值。 《法典》试图将文件“Company”物体初步化,这给我带来了其他问题。

最佳回答

As it is also receiving a value for “File.Company.Code” it is also attemting to bind the values to the File’s company object, which is what I want to avoid.

我假定,这意味着文件在你的项目内是一个domain model。 我认为,我建议采用view model

public class FileViewModel
{
    //other stuff contained within the File class

    [Required]
    public string FileCompanyCode { get; set: }
}

您可以利用您的看法模式,创造或重新计算您的实际经验。 文件张贴后存档。 仅仅没有打上你的实际档案公司对档案公司在观察模式中的财产表示反对。 如果您的档案公司编码受到约束或没有约束,那么实际上就没有了。

问题回答

I had a similar issue where I wanted the client-side validation but not the field being posted as it was a list of objects and the posting structure didn t support a normal validator.
Still, I was inspired by this question and answer and found out a solution where you add another input field (with all the HTML5 tags that an HTML.HiddenFor would have generated to enable unobtrusive validation) and Html.Validator for the non-existing model property hence the MVC binder in the postback would ignore it.
I also added an Html.ValidatorFor for the real property so that the validation on postback would have somewhere to render as my other validation tags point to a different tag (theoritically)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...