lobito lobito - 28 days ago 9
C# Question

Parsing a route parameter

Hello I am trying build a comment section for some physics articles in my site and for now i would like that when i click on

<a asp-route-articleid="smth" asp-action="Create">Create New</a>


it creates a page with url: http://localhost:65401/Physics/Create?articleid=smth

<h2>Create</h2>

<form asp-action="Create">
<div class="form-horizontal">
<h4>Physics</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CommentContext" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="CommentContext" class="form-control" />
<span asp-validation-for="CommentContext" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>


and now my question: How would I go about extracting that articleid in my controller? So far i tried this

public async Task<IActionResult> Create([Bind("ID,CommentContext,UserName,ArticleID")] Physics physics, string articleid)
{
if (ModelState.IsValid)
{
physics.UserName = HttpContext.User.Identity.Name;
physics.ArticleID = articleid;

_context.Add(physics);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(physics);
}


and also other desperate attempts by no luck so far. Any help would be appreciated.

EDIT:

using
HttpContext.Request.GetEncodedUrl();
gets me
http://localhost:65401/Physics/Create
but the point is I need that
articleid=smth
value.

Solution:

Request.Headers["Referer"].ToString();


will return string of full url including that 'articleid=smth' value.

Answer

If I understand I problem then you have following problem.

  1. You have Create Button on One Page and that generate link you described. http://localhost:65401/Physics/Create?articleid=smth

  2. Now when you click that link Your create method get called and it will return View that you have specified. ( At this time if you look articleId in your method then it will have that value).

  3. Now after you value in comment and you submit the form. At that time You will not able to find articleId. This is because articleId not preserve anywhere.

If above is your issue then following will solve your problem.

Solution.

In Controller create two method

Get Method that will called when you click Create New. Another one is called when you submit the form.

  1. Controller

    [HttpGet]
    public async Task<IActionResult> Create(string articleId)
    {
        Physics t = new Physics();
        if(!string.IsNullOrEmpty(articleId))
        {
            t.ArticleID = articleId;
        }
        return View(t);
    }
    
    [HttpPost]
    public async Task<IActionResult> Create([Bind("ID,CommentContext,UserName,ArticleID")]Physics physics)
    {
        if (ModelState.IsValid)
        {
            physics.UserName = HttpContext.User.Identity.Name;               
    
            //_context.Add(physics);
            //await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        return View(physics);
    }
    
  2. In your view I have made small change so articleId will preserve. ( See I have created hidden field for articleId)

<h2>Create</h2>
<form asp-action="Create">
    <div class="form-horizontal">
        <input asp-for="ArticleID" type="hidden" />
        <h4>Physics</h4>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="CommentContext" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="CommentContext" class="form-control" />
                <span asp-validation-for="CommentContext" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

I hope that this solution will help you.

Comments