रिटर्न व्यू (मॉडल) आपको त्रुटि देता है क्योंकि आप मॉडल को अपनी पोस्ट विधि में मानों के साथ भरते नहीं हैं और ड्रॉपडाउन के लिए मॉडल डेटा खाली है। त्रुटि को प्रदर्शित करने के तरीके को आगे बढ़ाने के तरीके के बारे में बताने के लिए कृपया विधि प्राप्त करें। त्रुटि के आदेश दिखाया जा सकता है में आप इस का उपयोग करना चाहिए:
[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>
}
आप सुनिश्चित हैं कि वापसी रीडायरेक्ट चाहते हैं और नहीं देख? – Grundy
'रिटर्न रीडायरेक्ट टॉएक्शन (" फॉर्म_एडिटिट "," फॉर्म ") के बजाय 'रिटर्न व्यू (मॉडल)' का प्रयास करें;' – Grundy
हां, मुझे रिटर्न रीडायरेक्ट टॉक्शन ("फॉर्म_एडिटिट", "फॉर्म") की आवश्यकता है; – Ramesh