2013-11-22 19 views
21

VS2010'da (MVC 4) Razor kullanarak bir C# projesi yapıyorum. Denetleyiciden Görüntülenecek ve Kullanıcıya gösterilecek hata iletisini döndürmem gerekiyor. Ne denedim idi:MVC 4-Denetleyici'den gelen hata iletisini göster - Göster in Görünüm

KONTROL:

[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"); 

} 

GÖRÜNÜM:

@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> 
} 

bunu başarmak için bana yardım edin.

+0

Eminseniz yönlendirmesi döndürmesine istiyor ve görüntüleyemezsiniz? – Grundy

+0

'Return' ('Form') yerine 'Return' ('Form') 'i döndürmek yerine View (model)' i döndürün; ' – Grundy

+0

Ya, RedirectToAction (" Form_edit "," Form ") iadesine ihtiyacım var; – Ramesh

cevap

27

Geri Döndürme Görünümü (model) size, döndürme yönteminizdeki değerleri içeren modeli doldurmadığınız ve açılır menü model verileri boş olduğundan hata verir. Lütfen hatayı görüntülemenin nasıl yönetileceğini daha ayrıntılı açıklamak için Get yöntemini sağlayın.

[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); 
} 

ve ardından görünümde sadece kullanın:: hata için gösterilmesini size bu kullanmalısınız

@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> 
} 

tamam çalışması gerekir.

Sadece RedirectToAction özelliğini kullanırsanız, sizi get yöntemine yönlendirirsiniz -> hata vermezsiniz ancak görünüm yalnızca yeniden yüklenir ve hiçbir hata gösterilmez. etrafında

diğer yolu ModelState.AddError tarafından değil hatayı geçebilir yani, ama böyle ViewData [ "hata"] ile:

[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 bir patron gibi çalışır! tnx – Reza

3

sen yapabilirsiniz bir yönlendirme yapmak istiyorsanız ya: tüm yanıtlar için

ViewBag.Error = "error message"; 

veya

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

teşekkürler.

ben aşağıdakileri yaparak bu çözmeyi başardı:

KONTROL:

[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); 
} 

GÖRÜNÜM:

@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> 
} 
İlgili konular