Home > Net >  How to redirect when viewmodel return nothing because the value is not equal
How to redirect when viewmodel return nothing because the value is not equal

Time:12-09

Because one of the value is not exist or equal between column, the viewmodel return nothing so i try to redirect with make another view with model only. Or maybe there is a way to open the view even one of the value is null? All solution will be accept

Thanks for your help!

// GET: Form/Details/5
        [Authorize]
        public ActionResult Details(int? ID)
        {
            if (ID == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Toolreq appRequest = db.Toolreqs.Find(ID);
            List<TooldateViewModel> RequestView = new List<TooldateViewModel>();
            var result = (from p in db.Toolreqs
                          join x in db.Toolpurposes on p.PurposeId equals x.PurposeId
                          join y in db.Toolcategories on p.CategoryId equals y.CategoryId
                          join z in db.Toollists on p.ToolId equals z.ToolId
                          join a in db.Toolgroups on p.Handle_by equals a.Handle_by
                          join o in db.pd_alltoy on p.Toy_Number equals o.pd_toynum
                          join c in db.pd_schedule on o.pd_projnum equals c.sch_projnum
                          where p.ID == ID && c.sch_attr == "FEP"
                          select new TooldateViewModel
                          {
                              ID = p.ID,
                              Status = p.Status,
                              Date_Request = p.Date_Request,
                              ToolId = z.ToolName,
                              Description = p.Description,
                              Qty_ = p.Qty_,
                              Date_Needed = p.Date_Needed,
                              Date_Done = p.Date_Done,
                              Underprint_Test_Result = p.Underprint_Test_Result,
                              Underprint_Test_Result_Image = p.Underprint_Test_Result_Image,
                              Underprint_Test_Result_Image_Path = p.Underprint_Test_Result_Image_Path,
                              Reference = p.Reference,
                              Handle_by = a.Useremail,
                              Toy_Number = o.pd_toynum,
                              Requestor = p.Requestor,
                              PurposeId = x.PurposeName,
                              CategoryId = y.CategoryName,
                              Sculpt_Mold_Number = p.Sculpt_Mold_Number,
                              Useremail = p.Requestor,
                              sch_pe = c.sch_pe,
                              sch_date = c.sch_date
                          }).ToList();
            if (result != null)
            {
                
                return View(result);
            }
            if (result == null)
            {
                return RedirectToAction("DetailsLess");
            }
            return View("");
        }

UPDATE!

"DetailsLess.cshtml" normal view without viewmodel that i want to redirect

@model LoginAndRegisterMVCMD5.Models.Toolreq
@{
    ViewBag.Title = "DetailsLess";
    Layout = "~/Views/Shared/_LayoutPage4.cshtml";

    if (Request.UrlReferrer != null)
    {
        string returnURL = Request.UrlReferrer.ToString();
        ViewBag.ReturnURL = returnURL;
    }
}

<div>
    <h4>Toolreq</h4>
    <hr />
    <dl >
        <dt>
            @Html.DisplayNameFor(model => model.Status)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Status)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Date_Request)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Date_Request)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Description)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Description)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Qty_)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Qty_)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Date_Needed)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Date_Needed)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Date_Done)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Date_Done)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Underprint_Test_Result)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Underprint_Test_Result)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Underprint_Test_Result_Image)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Underprint_Test_Result_Image)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Underprint_Test_Result_Image_Path)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Underprint_Test_Result_Image_Path)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Reference)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Reference)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Toy_Number)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Toy_Number)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Requestor)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Requestor)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Sculpt_Mold_Number)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Sculpt_Mold_Number)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Useremail)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Useremail)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Toolcategory.CategoryName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Toolcategory.CategoryName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Toolgroup.Useremail)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Toolgroup.Useremail)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Toollist.ToolName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Toollist.ToolName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Toolpurpose.PurposeName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Toolpurpose.PurposeName)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

CodePudding user response:

Because one of the value is not exist or equal between column, the viewmodel return nothing so i try to redirect with make another view with model only. Or maybe there is a way to open the view even one of the value is null?

Well, as per your shared code snippet it seems your appreach is correct. However, few things need to check and implemenet to make it work. Here are the details:

Firstly, Make sure, appRequest or result doesn't encounter any exception in that scenario your if (result == null) will not executed.

Secondly, Your @model LoginAndRegisterMVCMD5.Models.Toolreq on DetailsLess view must allow null result other than in between it will cought error and would not redirect.

Solution:

Your Toolreq model should allow null value just as below:

public class Toolreq
    {
        public string? Status { get; set; }
        public string? Date_Request { get; set; }
        public string? Description { get; set; }
        public int? Qty { get; set; }
    }

Note: Please add rest of the property based on your needs.

Controller:

Controller where you would redirect

public async Task<IActionResult> DetailsLess()
        {
            ViewBag.Message = "No Data Found By This ID";
            return View();
        }

   

Your Main Controller

        [Authorize]
        public ActionResult Details(int? ID)
        {
            //Set Try... Catch Block here if possibility of exception
            if (ID == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Toolreq appRequest = db.Toolreqs.Find(ID);
            List<TooldateViewModel> RequestView = new List<TooldateViewModel>();
            var result = (from p in db.Toolreqs)... ToList();//
          
            if (result == null)
            {
                return RedirectToAction("DetailsLess");
            }
            return View(result);
        }

Output:

enter image description here

Note: Make sure in your view of DetailsLess accept null model value @model LoginAndRegisterMVCMD5.Models.Toolreq. To do that you can allow null to all property Like this public string? Status.

  • Related