I need help with this. I have the code all set to allow users to input a text title and a text body and then aside a guid to that entry in the database.
The issue is that the guid changes and the text entry boxes go blank, and it needs to stay the same each time the submit button is pressed. My thought is that I should use the Html.BeginForm() to complete the task. Please let me know your thoughts.
html code
@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new{Id, title, body}))
Now i know that the
new {id}
should be
new{Id= Guid.NewGuid()}
but I need the controller to do assign the guid and keep it set and then have the BeginForm show the content that was placed in the text boxes with the guid in the Url.
controller
[HttpPost]
public ActionResult ACTION(NamedModel item)
{
if (ModelState.IsValid)
{
using (DB.DatabaseEntities db = new DB.DatabaseEntities())
{
DB.Model newRecord = new DB.Model();
newRecord.Title = item.Title;
newRecord.Body = item.Body;
newRecord.NamedId = Guid.NewGuid();
db.Models.Add(newRecord);
db.SaveChanges();
}
}
return View(item);
I basically need to know what I should put into the BeginForm().
Thank you.
I figured it out and wanted to share if anyone else was looking for this.
The Guid is set by the controller (but i have found that using the hmtl.Beginform makes no difference).
Now say you want to show the guid in the url when you submit something.
At the end of the controller action it should be
return RedirectAction("ActionName", "ControllerName", new {Id = Guid.NewGuid()}):
Just think of the URL and how that works, the returned value with the redirect will basically place everything the way it should look in the Url
So now my url is localhost####/ControllerName/Action/Guid
Give it a try if you cannot get the guid to show up on the Url
Thanks