2013-03-28 2 views
1

안녕하세요. 개발시 한 가지 문제가 있습니다.MVC 3 C# :: 양식 제출 후 양식 값 유지

난 그냥

내가 모든 필드

을 다시 내 제출 된 값을 원하는 같은 형태에 게시 한 후 어디 리디렉션하지 않으 MVC3에서 httppost 방법으로 내 양식을 제출 한 후 내 양식 데이터를 유지하려면

모든 설교?

나는 이것은 RAZOR 코드입니다 방법을

[AcceptVerbs(HttpVerbs.Post), ValidateInput(false)] 
    public ActionResult PostJobAction(FormCollection PostJobForm, HttpPostedFileBase uploadfile, JobsDetailModel objLocationModel) 
    { 
     bool blnSavedResume = false; 
     // JobsBusinessComponent objJobsBusinessComponent = new JobsBusinessComponent(); 
     JobsClient objJobsBusinessComponent = new JobsClient(); 
     UserModel objUserModel = null; 
     // byte[] bytArrayResume = null; 
     string strlogoImageName = string.Empty; 
     if (uploadfile != null) //if (uploadfile != null && uploadfile.ContentLength > 1024) 
     { 
      string UserFolderName = ConfigurationManager.AppSettings["LogoPath"].ToString() + objLocationModel.InstituteName.Trim(); 

      if (!Directory.Exists(UserFolderName)) 
      { 
       Directory.CreateDirectory(UserFolderName); 
      } 

      strlogoImageName = Guid.NewGuid().ToString() + "_" + uploadfile.FileName; 
      blnSavedResume = Utility.ResizeStream(Convert.ToInt16(ConfigurationManager.AppSettings["LogoSize"]), uploadfile.InputStream, UserFolderName + @"\" + strlogoImageName); 
      //using (MemoryStream ms = new MemoryStream()) 
      //{ 
      // var filePath = System.IO.Path.GetFileName(uploadfile.FileName); 
      // Image image = Utility.ScaleBySize(Image.FromFile(uploadfile.FileName, true), 200); 
      // uploadfile.InputStream.CopyTo(ms); 
      // bytArrayResume = ms.GetBuffer(); 
      //} 
      //// byte[] bytArrayResume = uploadfile.InputStream. 


      //blnSavedResume = objJobsBusinessComponent.SaveResumeOnDisk(UserFolderName, uploadfile.FileName, bytArrayResume); 
     } 
     if (blnSavedResume) 
     { 
      objUserModel = Utility.GetGuestUserData(); ; 
      //objLocationModel.StateID = objUserModel.StateID; 
      objLocationModel.CountryID = objUserModel.CountryID; 
      objLocationModel.LogoFileName = strlogoImageName; 
      objJobsBusinessComponent.PostJob(objLocationModel, 4); 
     } 
     TempData["Message"] = "Success"; 
     //return RedirectToAction("Index"); 
     if (ModelState.IsValid) 
     { 
      return View(objLocationModel); 
     } 
     else 
     { 
      // Dropdown selected values needs to be repopulated here , if there error in the model. 
      return View(objLocationModel); 
     } 
     // return View("~/Views/Employer/PostJob.cshtml"); 
    } 

다음의 처리를하고있는 중이 야. MVC에 완전히 익숙하지 않기 때문에 내가 잘못하고 있다면 제안 해주십시오.

@using (Html.BeginForm("PostJobAction", "PostJob", FormMethod.Post, new { enctype = "multipart/form-data", id = "PostJobForm", objPostJobModel = Model })) 
{ 

<div class="post_job" align="center" style="margin-left: 1px"> 
    <div id="tabs_container"> 
     <div class="my_profile_header"> 
      <img src="images/high_school.png" width="21" height="20" hspace="5" vspace="3" align="left" /><strong>Post 
       Job</strong> 
      <label style="float: right; color: Red; font-family: Calibri; font-style: italic; 
       margin-top: 10px"> 
       Fields marked with '*' are mandatory</label> 
     </div> 
     <div class="my_profile" id="InstituteDetail" style="display: block;"> 
      <div class="header_title_form"> 
       <img src="images/photo.png" width="24" height="24" hspace="5" vspace="2" align="left" /><div 
        align="left" style="vertical-align: middle; margin-left: 40px; margin-top: 3px; 
        font-weight: normal;"> 
        Institute Detail</div> 
      </div> 
     </div> 
     <div class="my_profile_container" align="center" style="background: none"> 
      <div class="clear"> 
      </div> 
      <div class="emolyer_box"> 
       <div align="center" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Institute Type:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left"> 
           @Html.DropDownListFor(x => x.InstituteType, (SelectList)ViewBag.InstituteType, "--Please Select Institute Type--", new { @class = "select", id = "ddlInstituteType", name = "InstituteType" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left" valign="middle"> 
           @Html.Label("Institute Category:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           <select name="InstituteCategory" id="ddlInstituteCategory" class="select" disabled="disabled"> 
           </select> 
           <img id="LoadingImage" style="width: 30px; height: 30px; vertical-align: middle; 
            visibility: hidden" src="../../Content/images/LoadingImage.gif" alt="Loading.." /> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Institute Name:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.InstituteName, new { id = "txtInstituteName", Name = "InstituteName", @class = "input" }) 
           <a style="text-decoration: none; color: Gray; cursor: default;" title="Subscription required for this feature"> 
            <u>Add More</u></a> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Zip Code:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.InstituteName, new { id = "txtZipCode", Name = "ZipCode", @class = "input" }) 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="emolyer_box"> 
       <div align="left" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Institute State:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.DropDownListFor(x => x.StateID, (SelectList)ViewBag.StateList, "---Please Select State---", new { @class = "select", ID = "ddlState", Name = "StateID" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left" valign="middle"> 
           @Html.Label("Institute City:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           <select name="CityID" id="ddlCity" class="select" runat="server"> 
           </select> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Institute Address:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextAreaFor(x => x.InstituteAddress, new { @class = "input", id = "txtInstituteAddress", style = "height:60px" }) 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="clear"> 
      </div> 
     </div> 
     <!-- End of Institute Div --> 
     <div class="my_profile" id="JobDetail" style="display: block"> 
      <div class="header_title_form"> 
       <img src="images/photo.png" width="24" height="24" hspace="5" vspace="2" align="left" /><div 
        align="left" style="vertical-align: middle; margin-left: 40px; margin-top: 3px; 
        font-weight: normal"> 
        Job Detail</div> 
      </div> 
     </div> 
     <div class="my_profile_container" align="center"> 
      <div class="clear"> 
      </div> 
      <div class="emolyer_box"> 
       <div align="center" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Job Title:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.JobTitle, new { id = "txtJobTitle", Name = "JobTitle", @class = "input" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left" valign="middle"> 
           @Html.Label("Role:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.DropDownListFor(x => x.RoleID, (SelectList)ViewBag.RoleList, "---Please Select Role---", new { @class = "select", ID = "ddlRole" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Subject:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.DropDownListFor(x => x.SubjectID, (SelectList)ViewBag.SubjectList, "---Please Select Subject---", new { @class = "select", ID = "ddlSubject" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Qualification:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.DropDownListFor(x => x.QualificationID, (SelectList)ViewBag.QualificationList, "---Please Select Qualification---", new { @class = "select", ID = "ddlQualification" }) 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="emolyer_box"> 
       <div align="left" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Job Type:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.DropDownListFor(x => x.JobType, (SelectList)ViewBag.JobTypeList, "---Please Select Job Type---", new { @class = "select", ID = "ddlJobType" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left" valign="middle"> 
           @Html.Label("Experience:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           <div class="clear"> 
           </div> 
           <div style="text-align: left"> 
            <select name="MinExperience" id="MinExperienceDropDown" style="width: 32%" class="select"> 
             <option value="">--Minimum--</option> 
             <option value="0">0</option> 
             <option value="1">1</option> 
             <option value="2">2</option> 
             <option value="3">3</option> 
             <option value="4">4</option> 
             <option value="5">5</option> 
            </select> 
            <select name="MaxExperience" id="MaxExperienceDropDown" style="width: 32%; float: right; 
             margin-right: 82px" class="select"> 
             <option value="">--Maximum--</option> 
             <option value="0">0</option> 
             <option value="1">1</option> 
             <option value="2">2</option> 
             <option value="3">3</option> 
             <option value="4">4</option> 
             <option value="5">5</option> 
            </select> 
           </div> 
           <div class="clear"> 
           </div> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Salary:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           <div class="clear"> 
           </div> 
           <div style="text-align: left"> 
            <select name="MinSalary" id="MinSalaryDropDown" style="width: 32%" class="select"> 
             <option value="">--Minimum--</option> 
             <option value="0">0</option> 
             <option value="1000">1000</option> 
             <option value="2000">2000</option> 
             <option value="3000">3000</option> 
             <option value="4000">4000</option> 
             <option value="5000">50000</option> 
            </select> 
            <select name="MaxSalary" id="MaxSalaryDropDown" style="width: 32%; float: right; 
             margin-right: 82px" class="select"> 
             <option value="">--Maximum--</option> 
             <option value="0">0</option> 
             <option value="1000">1000</option> 
             <option value="2000">2000</option> 
             <option value="3000">3000</option> 
             <option value="4000">4000</option> 
             <option value="5000">5000</option> 
            </select> 
           </div> 
           <div class="clear"> 
           </div> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("No Of Position:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.NoOfPosition, new { id = "txtNoOfPosition", Name = "NoOfPosition", @class = "input" }) 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="clear"> 
      </div> 
     </div> 
     <!-- End of Job Detail Div --> 
     <div class="my_profile" id="JobDescription" style="display: block"> 
      <div class="header_title_form"> 
       <img src="images/photo.png" width="24" height="24" hspace="5" vspace="2" align="left" /><div 
        align="left" style="vertical-align: middle; margin-left: 40px; margin-top: 3px; 
        font-weight: normal"> 
        Job Description<label style="color: Red">*</label></div> 
      </div> 
     </div> 
     <div class="my_profile_container" align="center"> 
      <div class="clear"> 
      </div> 
      <div class="emolyer_box"> 
       <div align="center" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left" id="HtmlEditor"> 
           @Html.TextAreaFor(x => x.JobDescriptionText, new { name = "inputHtmlEditor", id = "JobDescriptionText", rows = "5", }) 
           @*<textarea id="inputHtmlEditor" name="inputHtmlEditor" rows="5"></textarea>*@ 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="clear"> 
      </div> 
     </div> 
     <div class="my_profile" id="ContactInfo" style="display: block"> 
      <div class="header_title_form"> 
       <img src="images/photo.png" width="24" height="24" hspace="5" vspace="2" align="left" /><div 
        align="left" style="vertical-align: middle; margin-left: 40px; margin-top: 3px; 
        font-weight: normal"> 
        Contact Info</div> 
      </div> 
     </div> 
     <div class="my_profile_container" align="center"> 
      <div class="clear"> 
      </div> 
      <div class="emolyer_box"> 
       <div align="center" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Contact Person Name:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.ContactPersonName, new { id = "txtContPersonName", Name = "ContactPersonName", @class = "input" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left" valign="middle"> 
           @Html.Label("Phone No:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.InstitutePhoneNo, new { id = "txtInstitutePhoneNo", Name = "InstitutePhoneNo", @class = "input" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Logo On Home Page:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           <input type="checkbox" disabled="disabled" title="Subscription required for this feature" /> 
           <a style="text-decoration: none; color: Gray; cursor: default;" title="Subscription required for this feature"> 
            <u>Add/Edit Details</u></a> 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="emolyer_box"> 
       <div align="left" class="mailbox"> 
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("E-Mail:")<label style="color: Red">*</label> 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.ContactPersonEmailID, new { id = "txtContactPersonEmailID", Name = "ContactPersonEmailID", @class = "input" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left" valign="middle"> 
           @Html.Label("Web Site URL:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBoxFor(x => x.InstituteWebSite, new { id = "txtInstituteWebSite", Name = "InstituteWebSite", @class = "input" }) 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           &nbsp; 
          </td> 
         </tr> 
         <tr> 
          <td height="30" align="left"> 
           @Html.Label("Upload Logo Image:") 
          </td> 
          <td width="319" height="30" align="left" valign="middle"> 
           @Html.TextBox("uploadfile", "", new { @class = "input", id = "fileupload", type = "file" }) 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 
      <div class="clear"> 
      </div> 
     </div> 
     <input type="text" value="@TempData["Message"]" /> 
    </div> 
    <!-- End of contact info --> 
    <input type="button" class="green_btn" value="Save" disabled="disabled" style="cursor: default" 
     title="Subscription required for this feature" /> 
    <input type="submit" class="blue_btn" value="Post" /> 
    <input type="button" class="blue_btn" value="Cancel" /> 
    <input type="text" value="@TempData["Message"]" /> 
    @Html.Hidden("HiddenField", TempData["Message"]) 
</div> 

} 
+0

모델을 'objLocationModel'과 함께 표시하거나 ajax 게시물을 반환 할 수 있습니다. –

답변

2

채워진 모델로보기 만 반환하면됩니다. 문제는 당신이 ViewBags에서 온 페이지에 몇 DropDownList로가 있지만 그 ViewBags이 PostJobAction 재현되지 않습니다 그래서

return View(objLocationModel); 
+0

감사합니다 크리스, 하지만 내 CSHTML 파일에 그 값을 어떻게 바인딩 할 수 있습니까? 오류가 발생합니다. –

+0

@DhavalMarthak은 모델 구조와 마크 업을 보여줌으로써 더 많은 도움을받을 수 있습니다. 질문을 편집하고 추가 코드를 추가 할 수 있습니다. –

+0

일부 코드 단편을 추가했습니다. 내 질문을 다시 볼 수 있습니다. –

0
 [HttpPost] 
     public ActionResult PostJobAction(FormCollection PostJobForm, HttpPostedFileBase uploadfile, JobsDetailModel objLocationModel) 
     { 
      if(ModelState.IsValid) 
      { 
       return View(objLocationModel); 
      } 
      else 
      { 
       // Dropdown selected values needs to be repopulated here , if there error in the model. 
       return View(objLocationModel); 
      } 
     } 
+0

내 질문을 업데이트했습니다. 내가하고 싶은 것을 명확하게 알게 될 것입니다. –

+0

면도칼 코드를 공유 할 수 있습니까? 또한 어떤 컨트롤이 값을 받고 있습니까? 값을 잃어 버리는 것을 제어하는 ​​것. –

+0

코드를 다시 업데이트하고 면도기 파일 내용을 추가했습니다. –

1

좋아요. 하리 질라 (Hari Gillala)가 ViewBag를 사용하는 것이 좋지 않다는 것을 반복해서 알려줍니다.

하기의 처음 문제를 해결하자,이 같은 모델과 뷰를 반환 할 때 그 값을 유지합니다 모델에 바인딩 된 모든 컨트롤 :

[AcceptVerbs(HttpVerbs.Post), ValidateInput(false)] 
public ActionResult PostJobAction(FormCollection PostJobForm, HttpPostedFileBase uploadfile, JobsDetailModel objLocationModel) 
{ 
    return View(objLocationModel); 
} 

할 다음 일은이 값을하는 것입니다 당신의 드롭 다운에서 당신은 당신의 ViewBags를 다시 작성해야하므로 :

[AcceptVerbs(HttpVerbs.Post), ValidateInput(false)] 
public ActionResult PostJobAction(FormCollection PostJobForm, HttpPostedFileBase uploadfile, JobsDetailModel objLocationModel) 
{ 
    // do this code after you are done doing your stuff 
    // you always need to do this since you want to have the same form whether there is an error or your operation is a success 
    ViewBag.JobTypeList = create_this_list(); 
    ViewBag.InstituteType = = create_this_list_also(); 
    return View(objLocationModel); 
} 

을하지만 당신이 지금하고있는 일을 더 나은 방법이 그 귀하의 목록을 포함하여 몇 가지 속성을 포함하는 모델을 만드는 것입니다 :

// The Model 
public class JobActionFormModel { 
    public JobsDetailModel InputModel {get;set;} 
    public IEnumerable<JobType> JobTypes {get;set;} 
    public IEnumerable<InstituteType> InstituteTypes {get;set;} 
} 

// The way you build it 
[AcceptVerbs(HttpVerbs.Post), ValidateInput(false)] 
public ActionResult PostJobAction(FormCollection PostJobForm, HttpPostedFileBase uploadfile, JobsDetailModel objLocationModel) 
{ 
    var model = new JobActionFormModel { 
     InputModel = objLocationModel, 
     JobTypes = create_this_list(), 
     InstituteTypes = create_this_list_also(), 
    } 
    return View(objLocationModel); 
} 

// The way you consume it on your view 
<td width="319" height="30" align="left"> 
    @Html.DropDownListFor(x => x.InstituteType, new SelectList(Model.InstituteTypes, "Id", "Value", Model.InstituteType, new { @class="select" }) 
</td> 
// same goes for all your lists