开发者

How do I pass the number in the route through the view to my contreller

How do I pass the number in the route throught the view to my contreller

I call a route like

(localhost)/MyThing/Create/3

What I want to do is use that 3 param and set it in the MyThing object when I insert it into the database.

I created an Edit view that is strongly typed to my thing. So by default the view asks the user for this value (but I don' want to do that).

I have these methods in my controller

public ActionResult Create()
{
    var thing= new MyThing();
    return View(thing);
}

and

 [HttpPost]
        public ActionResult Create(MyThing newThing, int thingID)
        {
            if (ModelState.IsValid)
            {
           开发者_开发知识库     try
                {
                    newThing.ThingID= thingID;
                    DB.MyThing .AddObject(newThing);
                    DB.SaveChanges();

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Error creating new Thing");
                }
            }
            return View(newThing);
        } 

I tried commenting out the thingID code in the view but the I get an error that it can't be null. I also tried changing the first method here to take the param and set it when newing up the object but the route did work.

If I use the code above with the default view the number is editable and is zero on the screen. And when you get back to the controller the param is reset to 0.

What would be ideal is for the view to get this value (3 in my example) and show it but be read only. How would I do that?

I am using ASP.net 2 if it matters.


You have a problem with your post action as it has the thingId parameter twice: once as a property of the MyThing object and once as a separate parameter meaning that the following line is useless as the default model binder will already assign the property:

newThing.ThingID = thingID;

Now as far as your view is concerned you didn't show it but you could include this id parameter either as part of the form or as a hidden field.

For example:

<% using (Html.BeginForm(new { thingID = ViewContext.RouteData.Values["thingID"] })) { %>
    ...
<% } %>

And if you wanted to include it as readonly textbox:

<% using (Html.BeginForm()) { %>
    <%= Html.TextBoxFor(x => x.ThingId, new { @readonly = "readonly" }) %>
    ...
<% } %>

Finally your post action would become:

[HttpPost]
public ActionResult Create(MyThing newThing)
{
    if (ModelState.IsValid)
    {
        try
        {
            DB.MyThing.AddObject(newThing);
            DB.SaveChanges();
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", "Error creating new Thing");
        }
    }
    return View(newThing);
} 


I think you can just setup the route correctly and it will pass through when you call (localhost)/MyThing/Create/3

So in your Global.asax or Area route registration file, you would need to do something like this:

context.MapRoute("thing_route",
            "MyThing/Create/{thingID}",
            new
            {
                controller = "MyThing",
                action = "Create"
            }
        );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜