2013-11-22 6 views
21

मैं वीएस -2010 में रेज़र का उपयोग कर सी # प्रोजेक्ट कर रहा हूं। (एमवीसी 4)। मुझे नियंत्रक से त्रुटि संदेश वापस देखने और उपयोगकर्ता को दिखाने की आवश्यकता है। क्या मैं करने की कोशिश की गई थी:नियंत्रक से एमवीसी 4-रिटर्न त्रुटि संदेश- देखें

नियंत्रक:

[HttpPost] 
public ActionResult form_edit(FormModels model) 
{   
    model.error_msg = model.update_content(model);   
    ModelState.AddModelError("error", "adfdghdghgdhgdhdgda"); 
    ViewBag.error = TempData["error"]; 
    return RedirectToAction("Form_edit", "Form"); 

} 

देखें:

@model mvc_cs.Models.FormModels 
@using ctrlr = mvc_cs.Controllers.FormController 


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 

     <table> 
     <tr> 
     <td>  
     @Html.ValidationSummary("error")  
     @Html.ValidationMessage("error")  
     </td> 
     </tr> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.content_name) 
      @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 

     </th> 
    </tr> 
</table> 

<table> 
    <tr> 
     <td> 
      <input type="submit" value="Submit" /> 
     </td> 
    </tr> 
</table> 
} 

कृपया मेरी मदद करो इस लक्ष्य को हासिल करने के लिए।

+0

आप सुनिश्चित हैं कि वापसी रीडायरेक्ट चाहते हैं और नहीं देख? – Grundy

+0

'रिटर्न रीडायरेक्ट टॉएक्शन (" फॉर्म_एडिटिट "," फॉर्म ") के बजाय 'रिटर्न व्यू (मॉडल)' का प्रयास करें;' – Grundy

+0

हां, मुझे रिटर्न रीडायरेक्ट टॉक्शन ("फॉर्म_एडिटिट", "फॉर्म") की आवश्यकता है; – Ramesh

उत्तर

27

रिटर्न व्यू (मॉडल) आपको त्रुटि देता है क्योंकि आप मॉडल को अपनी पोस्ट विधि में मानों के साथ भरते नहीं हैं और ड्रॉपडाउन के लिए मॉडल डेटा खाली है। त्रुटि को प्रदर्शित करने के तरीके को आगे बढ़ाने के तरीके के बारे में बताने के लिए कृपया विधि प्राप्त करें। त्रुटि के आदेश दिखाया जा सकता है में आप इस का उपयोग करना चाहिए:

[HttpPost] 
public ActionResult form_edit(FormModels model) 
{   
    if(ModelState.IsValid()) 
    { 
     --- operations 
     return Redirect("OtherAction", "SomeController"); 
    } 

    // here you can use a little trick 
    //fill the model property that holds the information for the dropdown with the data 

    // you haven't provided the get method but it should look something like this 
    model.Countries = ... some data goes here; 
    model.dd_value = ... some other data; 
    model.dd_text = ... other data; 

    ModelState.AddModelError("", "adfdghdghgdhgdhdgda"); 
    return View(model); 
} 

और फिर ध्यान में रखते हुए बस का उपयोग करें:

@model mvc_cs.Models.FormModels 
@using ctrlr = mvc_cs.Controllers.FormController 


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 

    <table> 
    <tr> 
    <td>  
    @Html.ValidationSummary(true)    
    </td> 
    </tr> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.content_name) 
     @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 

    </th> 
</tr> 
</table> 

<table> 
<tr> 
    <td> 
     <input type="submit" value="Submit" /> 
    </td> 
</tr> 
</table> 
} 

यह ठीक काम करना चाहिए।

यदि आप केवल RedirectToAction का उपयोग करते हैं तो यह आपको प्राप्त विधि पर रीडायरेक्ट करेगा -> आपको कोई त्रुटि नहीं होगी लेकिन दृश्य को फिर से लोड किया जाएगा और कोई त्रुटि दिखाई नहीं देगी।

दूसरे के चारों ओर जिस तरह से है कि आप त्रुटि ModelState.AddError द्वारा नहीं पारित कर सकते हैं है, लेकिन ViewData [ "त्रुटि"] इस तरह के साथ:

[HttpPost] 
public ActionResult form_edit(FormModels model) 
{   
    TempData["error"] = "someErrorMessage"; 
    return RedirectToAction("form_Post", "Form"); 
} 

[HttpGet] 
public ActionResult form_edit() 
{ 
    do stuff here ---- 
    ViewData["error"] = TempData["error"]; 
    return View(); 
} 

@model mvc_cs.Models.FormModels 
@using ctrlr = mvc_cs.Controllers.FormController 


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 

    <table> 
    <tr> 
    <td>  
    <div>@ViewData["error"]</div>    
    </td> 
    </tr> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.content_name) 
     @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 

    </th> 
</tr> 
</table> 

<table> 
<tr> 
    <td> 
     <input type="submit" value="Submit" /> 
    </td> 
</tr> 
</table> 
} 
+0

ModelState.AddModelError एक बॉस की तरह काम करता है! tnx – Reza

3

आप एक रीडायरेक्ट, आप कर सकते हैं करना चाहते हैं तो या तो:

ViewBag.Error = "error message"; 

या

TempData["Error"] = "error message"; 
6

सभी उत्तर के लिए धन्यवाद।

मैं ऐसा करके इस समस्या के समाधान के लिए निम्न में सक्षम था:

नियंत्रक:

[HttpPost]  
public ActionResult form_edit(FormModels model) 
{ 
    model.error_msg = model.update_content(model); 
    return RedirectToAction("Form_edit", "Form", model); 
} 

public ActionResult form_edit(FormModels model, string searchString,string id) 
{ 
    string test = model.selectedvalue; 
    var bal = new FormModels(); 
    bal.Countries = bal.get_contentdetails(searchString); 
    bal.selectedvalue = id; 
    bal.dd_text = "content_name"; 
    bal.dd_value = "content_id"; 

    test = model.error_msg; 
    ViewBag.head = "Heading"; 

    if (model.error_msg != null) 
    { 
    ModelState.AddModelError("error_msg", test); 
    } 

    model.error_msg = ""; 
    return View(bal); 
} 

देखें:

@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 
    <table> 
    <tr> 
     <td> 
     @ViewBag.error 
     @Html.ValidationMessage("error_msg") 
     </td> 
    </tr> 
    <tr> 
     <th> 
     @Html.DisplayNameFor(model => model.content_name) 
     @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 
     </th> 
    </tr> 
    </table> 
} 
संबंधित मुद्दे