Niveditha Niveditha - 2 months ago 15
ASP.NET (C#) Question

MVC passing data from model to view

Actually I have a controller action and it gets the value of the variable 'result' from the function in a model. The action is as below.

if (model.buildingID == 0)
{
var result = objAddBuildingBusinessModel.AddBuilding(model, connectionstring,isUnique);
}
else
{
var result = objAddBuildingBusinessModel.UpdateBuilding(model, connectionstring);
}


So now based on the value of the variable 'result' I have to display a error message in the view if the value of variable 'result' is false. Where in view should I display that and how should I do it . Please help me as I am new to MVC

Answer

I think, you can create a model to keep same information then your data send to view any transfer method and show Exception.

EnumClass

Create Class EnumDefinitions. You can use all enum on this class inside. Write all enums in namespace. You can access this OperationEnum.FAIL or OperationEnum.SUCCESS

    [Obsolete("Dont Use", true)]
    internal static class EnumDefinitions
    {
       // dont use this.
    }


    /// <summary>
    /// Operation Enums
    /// </summary>
    public enum OperationEnum
    {
        FAIL = 0,
        SUCCESS = 1,
    }

Create Model

public class OperationResult
{
    /// <summary>
    /// Operation fail code
    /// </summary>
    public static string FAIL = "0";

    /// <summary>
    /// Operation success code
    /// </summary>
    public static string SUCCESS = "1";

    /// <summary>
    /// 1 : Success
    /// 0 : Fail 
    /// Maybe you can use Enum to set ResultCode.
    /// </summary>        
    public string ResultCode { get; set; }

    /// <summary>
    /// Exception message
    /// </summary>         
    public string ResultMessage { get; set; }


}

Catch Exception

Use code in try catch block and catch exception.Send data to view using any data transfer method ( ViewBag , vs vs ) or using jsonResult

 OperationResult resultData = new OperationResult();

    try
    {
      if (model.buildingID == 0)
      {
        var result = objAddBuildingBusinessModel.AddBuilding(model, connectionstring,isUnique);                       
      }
      else
      {
        var result = objAddBuildingBusinessModel.UpdateBuilding(model, connectionstring);
      }
      //resultData.ResultCode = OperationResult.SUCCESS;
      resultData.ResultCode = (int)OperationEnum.SUCCESS;
      resultData.ResultMessage = "Opetaion Success";
    }
    catch(Exception ex)
    {
       //resultData.ResultCode = OperationResult.FAIL;
       resultData.ResultCode = (int)OperationEnum.FAIL;
       resultData.ResultMessage = ex.Message;
    }
   // return resultData on viewBag or andy transafer data to view 
    ViewBag.ResultData = resultData;

Show Exception

Best practice get operation value to Enum ( success or fail ) You can use as examples hardcode or use enum value if use enum value so that you can reach through the same enum values ​​on both sides

if ('@ViewBag.ResultData.ResultCode' ==  '@(int)OperationEnum.FAIL') // or "2"
{
    var exMessage = '@ViewBag.ResultData.ResultMessage';
    alert(exMessage );
}