JH Ong
JH Ong

Reputation: 51

How to redirect the path with value?

My problem is, I'm trying to read my DB which has existing data "Code" and If it exists I want to redirect back to my save new item path('/GrpNew') by returning the key.

string query = "select gpcode from group where code = '" + sGrpCode + "'";
OdbcCommand cmd = new OdbcCommand(query, con);
MyDataReader = cmd.ExecuteReader();
try
{
    if (MyDataReader.Read())
    {
        bExist = true;
        ModelState.AddModelError("GrpCode", "Group code already exist");
        return RedirectToAction("GrpNew?GrpCode='" + sGrpCode + "'&'" + sGrpDesc + "' ");
    }
}

Mostly I use

("?GrpCode='" + sGrpCode + "'&'" + sGrpDesc + "' ")

for returning, but in asp.net I don't know how to do this or a proper way to return to that page without missing the key in value and show my validation "error"

This is my Model Below:

[StringLength(4)]
[Required(ErrorMessage = "Please enter group code.")]
public string GrpCode { get; set; }

[StringLength(30)]
[Required(ErrorMessage = "Please enter description.")]
public string GrpDesc { get; set; }

Please Help & Very Thank You

Upvotes: 0

Views: 109

Answers (1)

Simon C
Simon C

Reputation: 9508

Take a look at the RedirectToAction docs. You can specify an Action name, an optional Controller name, and an object of route values.

So in your case you can do this:

return RedirectToAction("GrpNew", new { GrpCode = sGrpCode , GrpDesc = sGrpDesc);

if the controller is different then it becomes

return RedirectToAction("GrpNew", "ControllerName", new { GrpCode = sGrpCode , GrpDesc = sGrpDesc);

Upvotes: 0

Related Questions