Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC3-post上的模型字段为空(使用viewmodel)_Asp.net Mvc 3_Post_Viewmodel - Fatal编程技术网

Asp.net mvc 3 MVC3-post上的模型字段为空(使用viewmodel)

Asp.net mvc 3 MVC3-post上的模型字段为空(使用viewmodel),asp.net-mvc-3,post,viewmodel,Asp.net Mvc 3,Post,Viewmodel,在MVC3应用程序中,我有一个具有奇怪行为的实体:创建操作工作正常,但编辑操作失败:提交时,我得到所有字段为空的模型 我搜索了有关此错误的建议,似乎ViewModel可能是原因,但我看不出我的有任何问题。正如我之前所说的,创作效果很好 请帮忙,谢谢 型号: public partial class tblEmployeur { [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessage

在MVC3应用程序中,我有一个具有奇怪行为的实体:创建操作工作正常,但编辑操作失败:提交时,我得到所有字段为空的模型

我搜索了有关此错误的建议,似乎ViewModel可能是原因,但我看不出我的有任何问题。正如我之前所说的,创作效果很好

请帮忙,谢谢

型号:

    public partial class tblEmployeur
{
    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredUserIDMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "UserIDDisplay")]
    public System.Guid userID { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredNomMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "NomDisplay")]
    public string nom { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredTypeSocMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "TypeSocDisplay")]
    public string type_soc { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredCodeRCMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "CodeRCDisplay")]
    public string codeRC { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "AdresseDisplay")]
    public string adresse { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredVilleMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "VilleDisplay")]
    public string ville { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredWilayaMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "WilayaDisplay")]
    public int wilaya { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "Tel1Display")]
    public string tel1 { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "Tel2Display")]
    public string tel2 { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "FaxDisplay")]
    public string fax { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredEmailMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "EmailDisplay")]
    public string email { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "SiteWebDisplay")]
    public string site_web { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredBanqueMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "BanqueDisplay")]
    public string banque { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredAgenceMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "AgenceDisplay")]
    public string agence { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredCompteMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "CompteDisplay")]
    public string nr_compte { get; set; }

    [Required(ErrorMessageResourceType = typeof(EmployeurResources), ErrorMessageResourceName = "RequiredDomaineMessage")]
    [Display(ResourceType = typeof(EmployeurResources), Name = "DomaineDisplay")]
    public int domaine { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "NotesDisplay")]
    public string notes { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "LogoPetitDisplay")]
    public byte[] logo_petit { get; set; }

    [Display(ResourceType = typeof(EmployeurResources), Name = "LogoGrandDisplay")]
    public byte[] logo_grand { get; set; }
}
视图模型:

public class EmployeurFormViewModel
{
    // Properties
    public tblEmployeur employeur { get; private set; }
    public SelectList Domaines { get; private set; }
    public SelectList Wilayas { get; private set; }
    public SelectList TypesSocietes { get; private set; }
    public string ActionToPerform { get; private set; }


    // Constructor
    public EmployeurFormViewModel(tblEmployeur unEmployeur, Guid employeurID, SelectList domList, SelectList Wils, SelectList typesSocsList)
    {
        employeur = unEmployeur;
         Domaines = domList;
         Wilayas = Wils;
         TypesSocietes = typesSocsList;

        if (String.IsNullOrEmpty(unEmployeur.userID.ToString())||(string.Compare(unEmployeur.userID.ToString(), "00000000-0000-0000-0000-000000000000")==0))
        {
            unEmployeur.userID = employeurID;
            ActionToPerform = "Create";
        }
        else
        {
            ActionToPerform = "Edit";
        }
    }
}
控制器:

    [Authorize(Roles = "Employeur")]
    public ActionResult Create(Guid id)
    {
        tblEmployeur employeur = new tblEmployeur();
        SelectList domainesList = new SelectList(db.tblDomaines, "domaine_ID", "domaine");
        SelectList wilsList = new SelectList(db.tblWilayas, "wilaya_ID", "wilaya");
        SelectList typesSocList = new SelectList(typesSocRepository.GetAll());
        return View("Create", new EmployeurFormViewModel(employeur, id, domainesList, wilsList, typesSocList));
    }

    //
    // POST: /Employeur/Create

    [HttpPost, Authorize(Roles = "Employeur")]
    public ActionResult Create(tblEmployeur employeur, Guid id)
    {

        SelectList domainesList = new SelectList(db.tblDomaines, "domaine_ID", "domaine", employeur.domaine);
        SelectList wilsList = new SelectList(db.tblWilayas, "wilaya_ID", "wilaya");
        SelectList typesSocList = new SelectList(typesSocRepository.GetAll(), employeur.type_soc);

        IEnumerable<System.Data.Entity.Validation.DbEntityValidationResult> validationResultsColl = db.GetValidationErrors();

        foreach (var validationResults in validationResultsColl)
        {
            foreach (var error in validationResults.ValidationErrors)
            {
                ModelState.AddModelError(error.PropertyName, new Exception(error.ErrorMessage));
            }
        }

        if (ModelState.IsValid)
        {
            try
            {
                repository.Add(employeur);
                repository.Save();
                return RedirectToAction("Details");
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        ModelState.AddModelError(validationError.PropertyName, new Exception(validationError.ErrorMessage));
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddOtherError(ex);
            }
        }
        return View("Create", new EmployeurFormViewModel(employeur, id, domainesList, wilsList, typesSocList));

    }

    //
    // GET: /Employeur/Edit/5


    public ActionResult Edit(Guid id)
    {
        tblEmployeur employeurCrt = db.tblEmployeurs.Find(id);
        SelectList domainesList = new SelectList(db.tblDomaines, "domaine_ID", "domaine", employeurCrt.domaine);
        SelectList wilsList = new SelectList(db.tblWilayas, "wilaya_ID", "wilaya", employeurCrt.wilaya);
        SelectList typesSocList = new SelectList(typesSocRepository.GetAll(), employeurCrt.type_soc);
        return View("Edit", new EmployeurFormViewModel(employeurCrt, id, domainesList, wilsList, typesSocList));
    }

    //
    // POST: /Employeur/Edit/5

    [HttpPost]
    public ActionResult Edit(tblEmployeur employeurCrt)
    {
        SelectList domainesList = new SelectList(db.tblDomaines, "domaine_ID", "domaine", employeurCrt.domaine);
        SelectList wilsList = new SelectList(db.tblWilayas, "wilaya_ID", "wilaya", employeurCrt.wilaya);
        SelectList typesSocList = new SelectList(typesSocRepository.GetAll(), employeurCrt.type_soc);

        IEnumerable<System.Data.Entity.Validation.DbEntityValidationResult> validationResultsColl = db.GetValidationErrors();

        foreach (var validationResults in validationResultsColl)
        {
            foreach (var error in validationResults.ValidationErrors)
            {
                ModelState.AddModelError(error.PropertyName, new Exception(error.ErrorMessage));
            }
        }

        if (ModelState.IsValid)
        {
            try
            {
                repository.Update(employeurCrt);
                repository.Save();
                return RedirectToAction("Details");
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        ModelState.AddModelError(validationError.PropertyName, new Exception(validationError.ErrorMessage));
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddOtherError(ex);
            }
        }
        return View("Edit", new EmployeurFormViewModel(employeurCrt, employeurCrt.userID, domainesList, wilsList, typesSocList));
     }
[授权(Roles=“Employeur”)]
公共操作结果创建(Guid id)
{
tblEmployeur employeur=新tblEmployeur();
SelectList domainesList=新的SelectList(db.tblDomaines,“域ID”,“域”);
SelectList wilsList=新的SelectList(db.tblWilayas,“wilaya_ID”,“wilaya”);
SelectList typesSocList=新的SelectList(typessocpository.GetAll());
返回视图(“创建”,新EmployeerFormViewModel(Employeer,id,DomainsList,wilsList,TypesScolist));
}
//
//职位:/Employeur/Create
[HttpPost,Authorize(Roles=“Employeur”)]
公共操作结果创建(tblEmployeur employeur,Guid id)
{
SelectList domainesList=新的SelectList(db.tblDomaines,“domaine_ID”,“domaine”,employeur.domaine);
SelectList wilsList=新的SelectList(db.tblWilayas,“wilaya_ID”,“wilaya”);
SelectList typesSocList=new SelectList(typessocposition.GetAll(),employeur.type_soc);
IEnumerable ValidationResultColl=db.GetValidationErrors();
foreach(validationResultsColl中的var validationResults)
{
foreach(validationResults.ValidationErrors中的var错误)
{
AddModelError(error.PropertyName,新异常(error.ErrorMessage));
}
}
if(ModelState.IsValid)
{
尝试
{
repository.Add(employeur);
Save();
返回重定向操作(“详细信息”);
}
捕获(System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
foreach(dbEx.EntityValidationErrors中的var validationErrors)
{
foreach(validationErrors.validationErrors中的var validationError)
{
AddModelError(validationError.PropertyName,新异常(validationError.ErrorMessage));
}
}
}
捕获(例外情况除外)
{
ModelState.AddOtherError(ex);
}
}
返回视图(“创建”,新EmployeerFormViewModel(Employeer,id,DomainsList,wilsList,TypesScolist));
}
//
//获取:/Employeur/Edit/5
公共操作结果编辑(Guid id)
{
tblemployers employeurct=db.tblemployers.Find(id);
SelectList domainesList=新的SelectList(db.tblDomaines,“domaine_ID”,“domaine”,employeerct.domaine);
SelectList wilsList=新的SelectList(db.tblWilayas,“wilaya_ID”,“wilaya”,employeerct.wilaya);
SelectList typesSocList=新的SelectList(typessocposition.GetAll(),employeerct.type_soc);
返回视图(“编辑”,新EmployeerFormViewModel(EmployeerCRT,id,DomainsList,wilsList,TypesScolist));
}
//
//职位:/Employeur/Edit/5
[HttpPost]
公共行动结果编辑(tblEmployeur EmployeerCRT)
{
SelectList domainesList=新的SelectList(db.tblDomaines,“domaine_ID”,“domaine”,employeerct.domaine);
SelectList wilsList=新的SelectList(db.tblWilayas,“wilaya_ID”,“wilaya”,employeerct.wilaya);
SelectList typesSocList=新的SelectList(typessocposition.GetAll(),employeerct.type_soc);
IEnumerable ValidationResultColl=db.GetValidationErrors();
foreach(validationResultsColl中的var validationResults)
{
foreach(validationResults.ValidationErrors中的var错误)
{
AddModelError(error.PropertyName,新异常(error.ErrorMessage));
}
}
if(ModelState.IsValid)
{
尝试
{
repository.Update(EmployeerCRT);
Save();
返回重定向操作(“详细信息”);
}
捕获(System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
foreach(dbEx.EntityValidationErrors中的var validationErrors)
{
foreach(validationErrors.validationErrors中的var validationError)
{
AddModelError(validationError.PropertyName,新异常(validationError.ErrorMessage));
}
}
}
捕获(例外情况除外)
{
ModelState.AddOtherError(ex);
}
}
返回视图(“编辑”,新的EmployeurFormViewModel(employeurCrt,employeurCrt.userID,DomainsList,wilsList,TypesClist));
}
编辑视图:

@model MyApp.ViewModels.EmployeurFormViewModel
@{
    ViewBag.Title = "Update employeur";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<div class="text">
    <h1>
        Gestion du compte employeur</h1>
    <div style="height: 5px">
    </div>
    @using (Html.BeginForm())
    {

        @Html.ValidationSummary(false, "Sauvegarde échouée. Veuillez corriger les erreurs et réessayer.")
        <p>@Html.ValidationMessage("_FORM")</p>

        <div class="validation-summary-errors">
            <span></span>
            <ul>
            </ul>
        </div>
        <fieldset style="width: 800px; line-height: 1.8em;">
            <legend>Update</legend>
            <table style="width: 100%; padding-bottom: 0; padding-top: 0; border: 1">
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.nom)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.nom, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.nom, "*")
                        @Html.HiddenFor(model => model.employeur.userID, new { @class = "input_txt_nofloat" })
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        @Html.DropDownList("employeur.type_soc", Model.TypesSocietes)
                        @Html.ValidationMessageFor(model => model.employeur.type_soc, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.codeRC)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.codeRC, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.codeRC, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.adresse)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.adresse, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.adresse, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.ville)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.ville, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.ville, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.wilaya)
                    </td>
                    <td style="border: 0;">
                        @Html.DropDownList("employeur.wilaya", Model.Wilayas, "Indiquez la wilaya", new { @style = "width: 232px;" })
                        @Html.ValidationMessageFor(model => model.employeur.wilaya)
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.tel1)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.tel1, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.tel1, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.tel2)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.tel2, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.tel2, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.fax)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.fax, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.fax, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.email)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.email, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.email, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.site_web)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.site_web, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.site_web, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.banque)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.banque, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.banque, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.agence)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.agence, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.agence, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.nr_compte)
                    </td>
                    <td style="border: 0;">
                        @Html.TextBoxFor(model => model.employeur.nr_compte, new { @class = "input_txt_nofloat" })
                        @Html.ValidationMessageFor(model => model.employeur.nr_compte, "*")
                    </td>
                </tr>
                <tr style="padding: 0 0 0 0; height: 32px">
                    <td style="width: 180px; border: 0;">
                        @Html.LabelFor(model => model.employeur.domaine)
                    </td>
                    <td style="border: 0;">
                        @Html.DropDownList("employeur.domaine", Model.Domaines, "Indiquez le domaine", new { @style = "width: 232px;" })
                        @Html.ValidationMessageFor(model => model.employeur.domaine, "*")
                    </td>
                </tr>
                <tr>
                    <td style="width: 180px; height: 60px; border: 0;">
                        &nbsp;
                    </td>
                    <td style="border: 0;">
                        <input type="submit" value="Sauvegarder" class="submit" />
                    </td>
                </tr>
            </table>
        </fieldset>
    }
    <div>
        @Html.ActionLink("Return", "Employeurs", "Home", null, new { @class = "link_no_underline" })
    </div>
</div>
@model MyApp.ViewModels.EmployeurFormViewModel
@{
ViewBag.Title=“更新员工”;
}
公司雇员签名
@使用(Html.BeginForm())
{
@ValidationSummary(false,“Sauvegardeéchouée.Veuillez corriger les erreurs et réessayer.”
@Html.ValidationMessage(“\u表单”)

更新 @LabelFor(model=>model.employeur.nom) @TextBoxFor(model=>model.employeur.nom,new{@class=“input\u txt\u nofloat”}) @Html.ValidationMessageFor(model=>model.employeur.nom,“*”) @Html.HiddenFor(model=>model.employeur.userID,新的{@class=“input\u txt\u nofloat”})
@model MyApp.ViewModels.EmployeurFormViewModel
[HttpPost]
public ActionResult Edit(tblEmployeur employeurCrt)
@model [YOUR-NAMESPACE-HERE].tblEmployeur
public tblEmployeur employeur { get; private set; }
public tblEmployeur employeur { get; set; }
public tblEmployeur employeur { get; private set; }
public ActionResult Create(tblEmployeur **employeur**, Guid id)
public ActionResult Edit(tblEmployeur **employeurCrt**)
public ActionResult Edit(tblEmployeur **employeur**)
public ActionResult Edit(EmployeurFormViewModel model) 
{ 
tblEmployeur employeurCrt = model.employeur;
...